Last Updated: February 25, 2016
·
786
· glitchmr

JavaScript classes template

// Constructor
function Animal(name) {
    this.name = name
}

// Method
Animal.prototype.move = function (meters) {
    alert(this.name + " moved " + meters + "m.")
}

// Constructor which inherits Animal
function Snake() {
    // super constructor
    Animal.apply(this, arguments)
}
Snake.prototype = new Animal

// Method
Snake.prototype.move = function (meters) {
    alert("Slithering...")
    // super
    Animal.prototype.move.apply(this, arguments)
}

// Create new Snake and move it
new Snake('Sammy the Python').move(5)