JS Inheritance

by Ivan Gerasimenko

JavaScript

var log = function(msg) { console.log(msg); };

// Superclass
function Actor(scene, x, y) {
    this.scene = scene;
    this.x = x;
    this.y = y;
    this.actorID = ++Actor.nextID;
}
Actor.nextID = 0; // static property of Actor

// Subclass
function Alien(scene, x, y, direction, speed, strength) {
    Actor.call(this, scene, x, y); // no need in NEW cause context is this
    this.direction = direction;
    this.speed = speed;
    this.strength = strength;
    this.alienID = ++Alien.nextID;    // Subclass can not have the same 
                                      // property as Superclass like ID-ID
}
Alien.nextID = 0;

var actor1 = new Actor("a", 13, 25);
var alien1 = new Alien("al", 22, 12, "alpha", 30, 15);

log( actor1 );
log( alien1 );