Recommended Posts


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 ?)

Share this post

Link to post
Share on other sites

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).

Share this post

Link to post
Share on other sites

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) = 'bar'

  onClick (event) {

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!

Share this post

Link to post
Share on other sites

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:

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.