Inheriting and parameters - why the double up?


#1


https://www.codecademy.com/en/courses/objects-ii/3/5?curriculum_id=506324b3a7dffd00020bf661#


Hi all, I'm a little confused with the Emperor constructor, why do I need to write
"name"
in the parameter and
'this.name = name;'
underneath, when the emperor inherits from animal (immediately below). Why doesn't it also inherit the name parameter? Why the double up?

Thanks!
Jeremy


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

// create your Emperor class here and make it inherit from Penguin
function Emperor(//why do I need 'name' here?>){
//why do I need to repeat this.name = name;?    
}

Emperor.prototype = new Penguin();

// create an "emperor" object and print the number of legs it has
var emperor = new Emperor("jessie");
console.log(emperor.numLegs);


#2

How else are you going to give you emperor a name?

Inheritance is very useful for inheriting member variables and methods. But name is depended for each instance:

# emperor instance of Emperor object
var emperor = new Emperor("jessie");

so for instance variable, inheritance is not particular useful


#3

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