lastnightsparty Posted November 4, 2014 Share Posted November 4, 2014 Hi guys, When I create a new state.js file for my game I simply start with: var gameState = { preload: function(){etc... but I've noticed that in most tutorials they use this syntax: var gameState = function(game){};gameState.prototype ={ preload: function(){etc... Both work in my games. But what is the difference between them? (especially since the first is shorter) Thanks for your help! Link to comment Share on other sites More sharing options...
eguneys Posted November 4, 2014 Share Posted November 4, 2014 Here's a similar question: http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work Link to comment Share on other sites More sharing options...
lastnightsparty Posted November 4, 2014 Author Share Posted November 4, 2014 Thank you. Yes, similar question, but no answer to the question yet: Why use a function statement for a STATE when a simple object will do? Link to comment Share on other sites More sharing options...
eguneys Posted November 4, 2014 Share Posted November 4, 2014 I don't know the exact answer, it just works and I roll with it, If you are curious, read the SO question and the answers to get in more depth. Basically when you assign properties to object.prototype you are doing Object oriented programming in javascript. prototype is useful for inheritance, google for oop, inheritance, javascript for more resources, here's one from MDN. Also you can checkout some books about javascript to get in more depth about the language. Here's a book for advanced javascript: here, and here's a list of books. Link to comment Share on other sites More sharing options...
Recommended Posts