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)
Written by Konrad Borowski
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#