18 Dry penguins


#1


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

As I understood this line Penguin.prototype = new Animal(); should copy function to Penguin constructor
Animal.prototype.sayName = function() {
console.log("Hi my name is " + this.name);
};
So I though it should work with my CaptainPing but it does not, what is not correct and how to change it?

// 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);
};
var ani = new Animal("Ani", 4);
// define a Penguin class
function Penguin(name){
    this.name = name;
    this.numLegs = 2;
}
ani.sayName()
var CaptainPing = new Penguin("lolka");
console.log(CaptainPing);
// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();

CaptainPing.sayName()


#2

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

var CaptainPing = new Penguin("lolka");

CaptainPing.sayName();     // Hi my name is lolka

Standard practice is to not use capitals on ordinary variables, only class names (constructors).

captainPing

#3

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