Last active
October 1, 2015 20:23
-
-
Save qetr1ck-op/4aed9017910b8e0d2c39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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