Black and White Penguin Magic


#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.numLegs = 2;
    this.name = name;
}
Penguin.prototype = new Animal();
var penguin = Penguin("penguin");
penguin.sayName();

// set its prototype to be a new instance of Animal

Note.Throwing an Error regarding Inheritence.
May be I was in wrong wrong direction.
Can any one post the correct code.so that I can rectify my mistake.


#2

this line:

var penguin = Penguin("penguin");

when you want to create an instance of your object, you need the new keyword:

var penguin = new Penguin("penguin");

#3

Thank You .It worked


#4