20. Penguins, Properties, and the Prototype


#1

What is wrong with my code?

Error message: "Oops, try again. Make sure to log the number of legs emperor has!"

function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}

// create your Emperor class here and make it inherit from Penguin
function Emperor(name) {
    this.name = name;
}
// create an "emperor" object and print the number of legs it has
Emperor.prototype = new Penguin();
var emperor = Emperor("Chris");
console.log(emperor.numLegs);

#2

@chrisvo33,
You will have to change your

into

var emperor = new Emperor("Chris");

=====================================================

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

#3

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