Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
Last active October 1, 2015 20:23
Show Gist options
  • Save qetr1ck-op/4aed9017910b8e0d2c39 to your computer and use it in GitHub Desktop.
Save qetr1ck-op/4aed9017910b8e0d2c39 to your computer and use it in GitHub Desktop.
// 1. constructor Animal
function Animal(name) {
this.name = name;
this.speed = 0;
}
// 1.1. prototype methods
Animal.prototype.stop = function() {
this.speed = 0;
alert( this.name + ' stops' );
}
Animal.prototype.run = function(speed) {
this.speed += speed;
alert( this.name + ' runs, the speed is ' + this.speed );
};
// 2. constructor Rabbit
function Rabbit(name) {
this.name = name;
this.speed = 0;
}
// 2.1. inhteritance
Rabbit.prototype = Object.create(Animal.prototype);
Rabbit.prototype.constructor = Rabbit;
//Rabbit.prototype.__proto__ = Animal.prototype; not supported in IE10+
// 2.2. Mehthods Rabbit
Rabbit.prototype.jump = function() {
this.speed++;
alert( this.name + ' jumps, the speed is ' + this.speed );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment