function Animal(species, alive, age) {
this.species = species;
this.alive = alive;
this.age = age;
}
function Dog(alive, age, likesCats) {
Animal.call(this, "Dog", alive, age);
this.likesCats = likesCats;
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
function Cat(alive, age, likesDogs) {
Animal.call(this, "Cat", alive, age);
this.likesDogs = likesDogs;
}
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.constructor = Cat;
var spike = new Dog(true, 2, true);
var fluffy = new Cat(true, 0.5, true);
// Let's check out what we have...
log(spike);
log(fluffy);
if(spike.likesCats && fluffy.likesDogs) {
log("Fluffy and Spike like each other");
} else {
log("Fluffy and Spike don't get along");
}
log("Spike is " + spike.age + " years old.");
log("Fluffy is a " + fluffy.species + ".");
<div id="log"></div>
External resources loaded into this fiddle: