Jump to content

From C# to JS


alalho
 Share

Recommended Posts

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

Link to comment
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).

Link to comment
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)

    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!

Link to comment
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: https://github.com/8Observer8/phaser-tutorial-examples-in-typescript

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...