Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sachinKumarGautam/c3b94475190bfde2d47aa23c46179cd1 to your computer and use it in GitHub Desktop.
Save sachinKumarGautam/c3b94475190bfde2d47aa23c46179cd1 to your computer and use it in GitHub Desktop.
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