18. Dry Penguins


#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();


I feel confused.

Why the constructor of Penguin need to have a para called name?
Penguin inherits Animal which have had the property called name.
Can't Penguin reuse it? Thx


#2

@zoe1992,

But in this exercise they are trying to encourage you to think about

J a v a S c r i p t and it's == p r o t o t y p e - C h a i n ==

They want you to set up the chain:

        OBJECT
  Object.prototype
           ^
           |
  Animal.prototype  (( has the sayName Method))
           |                              ^
           |                              | 
           |                    Animal Class-Constructor
           ^                   | with this constructor
           |                   |  you can create Instance's of Animal
           |
           |    <<== You define this link in the Chain using
           ^            Penguin.prototype = new Animal();
           |
  Penguin.prototype ((is empty))
           |               ^
           |               | 
           |          Penguin Class-constructor
           ^                   | with this constructor
           |                   |  you can create Instance's of Penguin
   /Instance /      <---   using new Penguin("aName");
   | of      |
   | Penguin |
    The Penguin-Instance has access to Animal.prototype.sayName Method
     via the Penguin-prototype which is  -chained- to the Animal-prototype.

++++++++++++++++++++++++++++++
IMHO you 'get' property-Value's
but you cannot 'set' property-Value's

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; <<Removed for Test-purpose
 this.numLegs = 2;
 };

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

Now we do...

 var peg = new Penguin("testname");
 console.log( peg );  //{ numLegs: 2 }
 //peg.name = "test";
 console.log( peg.name );  //undefined
 var ani = new Animal("testAni",4);
 console.log (ani); //{ name: 'testAni', numLegs: 4 }
 peg.name = "test";
 console.log( peg.hasOwnProperty('name') ); //true
 console.log (ani); //{ name: 'testAni', numLegs: 4 }
 console.log(peg); //{ numLegs: 2, name: 'test' }

As you can see, the name property with property-key name and its associated String Value 'test'
was added to the peg object....


#3

I agree, it seems like a confusing question/subject. Not sure how to improve it. I don't know if i am getting a head of myself here. I think they need to use a better subject as I would think when building this you would just put a category or species to the Animal Class which solves this. It all just does not seem that dry, we are still writing 3 lines of code for a penguin class.. where we set a name and num legs? What am i missing?