Jump to content

March releases of Firefox and Chrome to support class, extends, constructor, super keywords in Javascript


samlancashire
 Share

Recommended Posts

Firefox 45 and Chrome 49 (both to be released in March) will have support for ECMAScript 6's class declaration, as well as constructor method, and super and extends keywords. This means we can now create classes in Javascript like this:

class Sprite {
    constructor(health) {
        this.health = health;
        this.maxHealth = health;
    }
    get totalDamageTaken() {
        return this.maxHealth - this.health;
    }
}

class Enemy extends Sprite {
    constructor(health, damage) {
        super(health);
        this.damage = damage;
    }
    attack(sprite) {
        sprite.health -= this.damage;
    }
}

var player = new Sprite(10);
var badguy = new Enemy(10, 2);

badguy.attack(player);
badguy.attack(player);

console.log(player.health, player.totalDamageTaken); // 6, 4

If you want to try for yourself you can download Firefox Developer Edition, as it currently supports these features.

For more information read MDN's documentation.

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