jjcale Posted February 24, 2017 Share Posted February 24, 2017 Hi, I have seen two approaches to encapsulate Phaser functions into objects: 1) in a state object directly 2) in a state object's prototype What are the benefits of each approach? I assume that I would create only one instance of the state object, so there is little memory overhead (created instance + class object itself). I have no plans for inheritance. Thanks, JJ Link to comment Share on other sites More sharing options...
Arcanorum Posted February 24, 2017 Share Posted February 24, 2017 As far as I'm aware there isn't any noteworthy difference. It comes down to style. Link to comment Share on other sites More sharing options...
jjcale Posted February 24, 2017 Author Share Posted February 24, 2017 Yes, I agree, with ES6, it's looks stylish. Link to comment Share on other sites More sharing options...
drhayes Posted February 24, 2017 Share Posted February 24, 2017 You're correct -- the main use of modifying a particular function's prototype is to share the memory taken by those functions across a lot of instances of that function's prototype. Not making more than one? Not doing inheritance? Totally a style thing. ( = Link to comment Share on other sites More sharing options...
mattstyles Posted February 25, 2017 Share Posted February 25, 2017 Although in this case it probably makes very little difference as an app gets complex you're hitting on a very contentious issue in JS currently regarding class syntax, hierarchies, prototypal inheritance and object linking. This is a nice gist showing object linking (which is actually all JS has, the es2015 class syntax is a big fat red herring and will always be so in a dynamic language). The biggest writer in this field is Kyle Simpson (@getify), who wrote the You Dont Know JS books, but it touches on Crockford's opinions, how Brendan Eich thought JS should evolve and Eric Elliott is a big proponent of shunning classical-shims for JS 'inheritance'. Some more info: http://akonwi.io/blog/oloo (this is a very brief rehash of Kyle Simpson's stuff, I'd recommend getting the YDKJS books for more info, or trawl through his blog, most of the book content is covered there in one way or another) https://medium.com/javascript-scene/10-interview-questions-every-javascript-developer-should-know-6fa6bdf5ad95#.h9vtxj7e5 this shows how important Eric Elliott believes structure to be by only hiring people who understand it! There are also a raft of great links to other articles on the subject in there. https://medium.com/@cscalfani/goodbye-object-oriented-programming-a59cda4c0e53#.6ubbgc641 Quote "Favour object composition over class inheritance" GOF Quote "The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle." Joe Armstrong jjcale 1 Link to comment Share on other sites More sharing options...
Recommended Posts