JavaScript - Prototypical Inheritance
by johnpapa
JavaScript
function Animal(food) {
this.food = food;
};
Animal.prototype.feed = function() {
console.log('Fed the animal ' + this.food);
};
var animal = new Animal('Strawberries');
animal.feed();
var test = animal instanceof Animal;
console.log(test);
function Cow(color){
this.color = color;
};
Cow.prototype = new Animal('Hay');
var cow = new Cow('White');
cow.feed();
var test2 = cow instanceof Animal;
console.log(test2);
console.log(cow);
console.log(cow.color);
console.log(cow.food);