19.Black(and White) Penguin Magic


#1


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


it says: Oops, try again. It looks like you have an error in your code. Here's the message: 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("Zovs");
penguin.sayName( );
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();


#2

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("Zovs");
penguin.sayName( );

At this point in the code Penguin has no sayName method let alone a sayName property. This feature is part of Animal and currently there is no connection between them so the penguin.sayName() call fails.

With the very next line:
Penguin.prototype = new Animal();

you establish this connection and after this the same call of the sayName method might work.


#3

Great! It worked. thanks


#4

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