-
-
Save sachinKumarGautam/c3b94475190bfde2d47aa23c46179cd1 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