Black and White Penguin Magic


Ok I'm getting sick of not know every single exercise one after the other.... Anyone else have this issue? So far it's taken me roughly 50 hours to do this "10 hour" Javascript program.

can someone walk me through the last portion of this code? I'm not understanding how anything correlates with each other and why the Penguin function is redundant in the fact that we could just make a new object with input for the parameters instead of writing the whole thing out again.

// the original Animal class and sayName method
function Animal(name, numLegs) { = name;
    this.numLegs = numLegs;
Animal.prototype.sayName = function() {
    console.log("Hi my name is " +;

// define a Penguin class
function Penguin(name) { = name;
    this.numLegs = 2;

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


In the last section of your code you create an instance of the parent class Animal that inherits all of the parent class' attributes. But instead of using the Animal class alone you use your child class Penguin to create a prototype that has only one parameter and that is (name) then you also call your prototype with the already defined function sayName which is a prototype of the Animal class.

Penguin.prototype = new Animal();
var penguin = new Penguin("Jack");


so the Penguin class inherits the Animal method, in which I don't have to type it twice?


Well yes, but what exactly are you typing twice?


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