20.Penguins,Properties, and the Prototype


#1



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

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


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

function Emperor(name){
    this.name = name;
}

Emperor.prototype = new Penguin();

var pengu = new Emperor("Pengu");
console.log(pengu.numLegs);.


#2

Hint: The last line has a syntax error.


#3

@kevkevlarocque,
In the instructions

Create a new emperor object
that is an instance of the Emperor class with any name you'd like.
Then use console.log to print the number of legs emperor has
—this should have been inherited from Penguin!

You are creating a pengu object !!!

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  (( is Empty))
           |                              ^
           |                              | 
           |                    Penguin Class-Constructor
           ^                   | with this constructor
           |                   |  you can create Instance's of Penguin
           |                   | has a numLegs property
           |    <<== 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 numLegs property
     via the Emperor-prototype which is
    -chained- to the Penguin-prototype

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

function Emperor(name){
    this.name = name;
};

Emperor.prototype = new Penguin();

var emperor = new Emperor("Caesar");

console.log(emperor.numLegs);

Wow, this is incredibly annoying. What the ■■■■ is wrong with my code?!


#5

Your code is working on exercise 20. Try clearing cookies/caches.


#6

What is wrong with this:

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;
};
Emperor.prototype = new Penguin();

// create an "emperor" object and print the number of legs it has
var emperor = new Emperor("Hi");
console.log(emperor.numLegs);

#7

Never mind. I refreshed the browser and it was fine :expressionless:


#8