Penguins, Properties, Prototype... typing the same exact thing?


#1

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

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

Is there a way to create an Emperor class without copying and pasting all of that Penguin constructor code? I know there must be..


#2

I suppose you could make a constructor within a constructor but that would be messy :slightly_smiling: .

if both constructors are the same I would just delete one of them. Otherwise you can add optional parameters for different types of penguins like so..

function Penguin(type, name, age) {
    this.type = type || "not specified";
    this.name = name;
    this.age = age || "not specified";
    this.numLegs = 2;

}

// create your Emperor class here and make it inherit from Penguin

Emperor = new Penguin("", "Emporor", "17");

Now this is ok, but what they have showed you is correct. Just remember that they are teaching you a concept; not the bigger picture of how this would actually be useful in a real work environment. You probably wouldn't create a constructor that inherits from a parent constructor with the same parameters in a real program.