15.19: Black (and White) Penguin Magic (penguin.sayName is not a function)


#1


In 15.19: Black (and White) Penguin Magic


It does not let me pass and says: TypeError: penguin.sayName is not a function


// 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
}
var penguin = new Penguin("Cap");

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


#2

Insert Penguin.prototype = new Animal(); before you instantiate from Penguin object to make sure you inherit properties from Animal object before usage.


#3

I think I got it. Just to be clear, the problem was that I was declaring penguin before inheriting the functions right?


#4

Yes, you're right. You need to inherit properties first and then you can instantiate object and now you can use those properties that Animal object has.


#5

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