Created
April 18, 2018 05:18
-
-
Save lusan/8326664ca2cb38af9b425d33ed5dfac1 to your computer and use it in GitHub Desktop.
This file contains 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
function Animal(name) { | |
this.name = name; | |
} | |
// Example method on the Animal object | |
Animal.prototype.getName = function() { | |
return this.name; | |
} | |
function Mammal(name, hasHair) { | |
// Use the parent constructor and set the correct `this` | |
Animal.call(this, name); | |
this.hasHair = hasHair; | |
} | |
// Inherit the Animal prototype | |
Mammal.prototype = Object.create(Animal.prototype); | |
// Set the Mammal constructor to 'Mammal' | |
Mammal.prototype.constructor = Mammal; | |
Mammal.prototype.getHasHair = function() { | |
return this.hasHair; | |
} | |
function Dog(name, breed) { | |
// Use the parent constructor and set the correct `this` | |
// Assume the dog has hair | |
Mammal.call(this, name, true); | |
this.breed = breed; | |
} | |
// Inherit the Mammal prototype | |
Dog.prototype = Object.create(Mammal.prototype); | |
// Set the Dog constructor to 'Dog' | |
Dog.prototype.constructor = Dog; | |
Dog.prototype.getBreed = function() { | |
return this.breed; | |
} | |
var fido = new Dog('Fido', 'Lab'); | |
fido.getName(); // 'Fido' | |
fido.getHasHair(); // true | |
fido.getBreed(); // 'Lab' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment