ixm1oa
Last Updated: February 25, 2016
·
638
· glitchmr
Be0473b0966b7766822d4316c6bf9106

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)
Say Thanks
Respond