Penguins, Properties, and the Prototype (please clarify inheriting)


#1


Why do I have to define the name property in Emperor?
Why doesn't Emperor inherit the name property from Penguin when I set the Emperor class to be an instance of Penguin?
If this.numLegs = 2 is inherited? It seems that the name argument that gets passed through the Emperor constructor should set this.name of Emperor (by way of the Penguin inheritance) would not necessitate setting Emperor's this.name property (as shown). It doesn't, but why?

function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}

// create your Emperor class here and make it inherit from Penguin
var Emperor = function (name) {
    this.name = name;
    };
Emperor.prototype = new Penguin();

// create an "emperor" object and print the number of legs it has

var emperor = new Emperor("Joe");
console.log(emperor.name);
console.log(emperor.numLegs);


#2

Actually it does. But consider what value it was given:

> console.log(Emperor.prototype)
Penguin { name: undefined, numLegs: 2 }

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.