lastnightsparty Posted November 4, 2014 Report 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! Quote Link to comment Share on other sites More sharing options...
eguneys Posted November 4, 2014 Report Share Posted November 4, 2014 Here's a similar question: http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work Quote Link to comment Share on other sites More sharing options...
lastnightsparty Posted November 4, 2014 Author Report 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? Quote Link to comment Share on other sites More sharing options...
eguneys Posted November 4, 2014 Report 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.