Black (and White) Penguin Magic , confused


#1

// 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 = "Olov";

penguin.sayName();

Not sure what exactly went wrong? Any help appreciated.


#2

Hi Look well how you created you new penguin this part

var penguin = "Olov";

#3

var penguin = new Penguin("Olov");

This works now! Thanks for the fast reply, I was a bit confused :wink:


#4

I can't find where is my mistake... it's giving me Reference error

// 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
// 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 penquin = new Penquin("Alex");
penquin.sayName();


#5

HI thid part

var penquin = new Penquin("Alex");

You rote penquin instead of penguin


#6

Try this code:

// 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('Chilly Willy')
penguin.sayName();