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


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 of Emperor (by way of the Penguin inheritance) would not necessitate setting Emperor's property (as shown). It doesn't, but why?

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

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

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

var emperor = new Emperor("Joe");


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

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


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