Up the Food-I-mean-Prototype Chain


#1


I have a question: Why do we define "name" property in Penguin and Emperor class again, while it is defined in Animal class, that is superclass of Penguin and Emperor. Thanks.


function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
    this.isAlive = true;
}
function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}
function Emperor(name) {
    this.name = name;
    this.saying = "Waddle waddle";
}

// set up the prototype chain
Penguin.prototype = new Animal();
Emperor.prototype = new Penguin();

var myEmperor = new Emperor("Jules");

console.log( myEmperor.saying ); // should print "Waddle waddle"
console.log( myEmperor.numLegs ); // should print 2
console.log( myEmperor.isAlive ); // should print true


#2

This post was flagged by the community and is temporarily hidden.


#3

I mean, "name" property, which can Penguin and Emperor inheritance from superclass Animal ? Thanks for your answer!!!