alalho Posted October 28, 2017 Share Posted October 28, 2017 Hi, I'm coming from Unity (C#), I've followed some JS tutorials, but I can't find really clear answers about OOP. Because many js online documentation talks about web developpement, I don't know if it's better to use CLASSES over PROTOTYPES in javascript game dev ? What th pros, what the cons ? (Or where can I find answers about ?) Quote Link to comment Share on other sites More sharing options...
bruno_ Posted October 29, 2017 Share Posted October 29, 2017 Classes in javascript are new to ES6 (newer javascript). In ES5 (older javascript), you would use prototypes to achieve the same functionality of classes. The difference now is that not all browsers support full ES6 yet. If you're making a game that will run on newer versions of browsers, you can use ES6 and classes. If not, you can write ES5 directly, or write ES6 and transpile it to ES5 (view some examples on how to do this on google). Quote Link to comment Share on other sites More sharing options...
mattstyles Posted October 31, 2017 Share Posted October 31, 2017 Should probably note that there are no proper 'classes' in JS, it is prototypal, not classical, the newer ES6 class keyword does not help as its not a class, just sugar over a method of making a prototypal system seem classical. As an example, a programmer from a classical background might assume the following: // Warning: potential unexpected result ahead! class Foo { constructor () { var el = document.querySelector('body') el.addEventListener('click', this.onClick) this.name = 'bar' } onClick (event) { console.log(this.name) } } new Foo() // Click the body element // logs 'bar' Due to the usual scoping rules for classes prevalent in other languages you might assume the code above will work and log out 'bar' when clicking the body element. However, it won't, and it won't even break, it'll just log 'undefined'. This is due to JS scoping rules. In this case the Foo::onClick will become scoped to the HTMLElement it has been attached to, which probably doesn't have a `name` member. There are other gotchas, some related to scoping, some not. Depending on what you want to do none of these may become an issue and classes will work how you expect even though underneath they may not be doing what you are comfortable with from other languages. For example, using prototypes in the example above won't help at all, it's only that you may not have a preconception as you may not have used a prototypal language before and you wouldn't be using the `class` keyword to declare a structure. In truth, if all you're worried about is classes vs prototypes then you're laughing! Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 3, 2017 Share Posted November 3, 2017 C# was made by Anders Hejlsberg He was made TypeScript language also that added to JS key words: interface, private/protected/private and another OOP stuffs like in C#. 3D framework Babylon.js was written in TypeScript. If you want to make 2D games this is a good tutorial for start: Advanced Phaser and TypeScript Projects You can download a source code from here: https://github.com/8Observer8/phaser-tutorial-examples-in-typescript 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.