18. DRY Penguins (Why does this print "{}"?)


#1

// the original Animal class and sayName method
function Animal(name, numLegs) {
this.name = name;
this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};

// define a Penguin class
function Penguin(name, numLegs) {
this.name = name;
this.numLegs = 2;
}

// set its prototype to be a new instance of Animal
Penguin.prototype.sayName = function() {

}
Penguin.prototype = new Animal();

Why does this print "{}"?


#2

The javascript interpreter always evaluates and prints your last line for debugging purposes. In this case, it's Penguin.prototype = new Animal();


#4

Follow this code :point_down:

// define a Penguin class
function Penguin(name) {
this.name = name;
this.numLegs = 2;
}

// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();