Is it possible to get Dog and Cat to inherit from Animal (without using the class or extends keywords), without editing the code that has already been written, and while keeping all new code within the current constructor functions?
function Animal() {
this.sleep = function() {
var hoursSleepPerNight = this.hoursSleepPerNight;
console.log( "z".repeat(hoursSleepPerNight) );
}
}
function Dog() {
this.hoursSleepPerNight = 8;
}
function Cat() {
this.hoursSleepPerNight = 7;
}
var dog = new Dog();
dog.sleep();
// "zzzzzzzz"
Dog.prototype = Object.create(Animal.prototype), but note thatsleepis an own property, it won't be inherited. You could tryDog.prototype = new Animal()but be aware of side-effects.class+extends, not different than in Python. Also you usually would define (prototype) methods after doing the inheritance?