Inheritance
"Objects obtaining features from parent objects"
For example an Animal constructor may have a prototype "move", as all animals can move. When a new animal type is made it can inherit all the properties of Animal but then additional properties can be added. For example a dog can also bark and cat meow, but they both inherit the move prototype.
// Inheritance
function Animal() {
this.init = function(name) {
this.name = name;
};
}
Animal.prototype.move = function () {
return true;
};
function Dog(name) {
this.init(name);
}
Dog.prototype = new Animal();
Dog.prototype.bark = function () {
return true;
};
function Cat(name) {
this.init(name);
}
Cat.prototype = new Animal();
Cat.prototype.meow = function () {
return true;
};
var bonnie = new Dog('bonnie', 12);
var minnie = new Cat('minnie', 5);
// true
console.log(bonnie.move());
// true
console.log(bonnie.bark());
// is not a function
console.log(bonnie.meow());
// true
console.log(minnie.meow());