Black and White Penguin Magic


#1


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

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


#2

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

#3

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


#4

Well yes, but what exactly are you typing twice?


#5

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