Black (and White) Penguin Magic


#1


I was wondering - if the Penguin class is meant to 'inherit' the same elements as the Animal class, why do we need to input the 'name' and 'numLegs' into the Penguin class?


// the original Animal class and sayName method
function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}
Animal.prototype.sayName = function() {
    console.log("Hi my name is " + this.name);
};

// define a Penguin class
function Penguin (name) {
    this.name = name,
    this.numLegs = 2
}

// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();

var penguin = new Penguin("Charlie");
penguin.sayName();


#2

I think this is coming in the next lessons...


#3

Inheritance builds on the prototype. You can increase its complexity, but you can't decrease it.


#4

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