19.Black (and White) Penguin Magic


#1


console: TypeError: penguin.sayName is not a function
popup window: Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: penguin.sayName is not a function


Help i do not get what`s wrong

// 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(name,this.numLegs);
var penguin = new Penguin("liam");

penguin.sayName();


#2

// set its prototype to be a new instance of Animal
penguin.prototype = new Animal(name,this.numLegs); // you can't set property 'prototype' of undefined!!

Should be:

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

#3

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