20/30 I have no idea where Im going wrong here


#1

//Here is my code,whats wrong here:
function Penguin(name) {
this.name = name;
this.numLegs=numLegs;

}

// 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();
console.log(emperor.numLegs);


#2

My code look exactly the same as yours except for the Penguin function. Where you have this.numLegs = numLegs; I have this.numLegs = 2;. I don't know if that will fix your problem but give it a shot.


#3

Have the same problem here, I've set numLegs equal to 2,but it still gives the problem "Oops, try again. Make sure to log the number of legs emperor has! " . Hope somebody will take attention to this


#4

Well I've found the solution - just open the lesson in a new tab - it will work.I think there is some problem with js shell or smthng


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

// 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();
console.log(emperor.numLegs);

The immediate error thrown is,

ReferenceError: numLegs is not defined

This traces back to the Penguin constructor, which should read,

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

Now when we log emperor.name and emperor.numLegs we get,

undefined
undefined

This is because the emperor instance has no supplied data. We need to write the constructor to set both values:

function Emperor(name){
    this.name =  name;
    this.numLegs = 2;
}
Emperor.prototype = new Penguin(); // notice no arguments

var emperor = new Emperor("Napolean");

console.log(emperor.name); // Napolean
console.log(emperor.numLegs); // 2

#6

The class Emperor is not supposed to have a numLegs property 'cause it inherits it from the Penguin Class.


#7

Emperor only inherits from Penguin.prototype, not from Penguin. The only way to have direct properties on an Emperor instance is to define them in the Emperor constructor. Prove this wrong.


#8

@arcrockstar12762 is right.

JavaScript is prototype-based, and I don’t really understand what you mean here:

Emperor only inherits from Penguin.prototype, not from Penguin.

We don’t have to specify the same properties in different classes. It would make whole inheritance model completely unpractical, don’t you think? I mean, if our class is not able to inherit properties or methods it does not make any sense to inherit at all.

Example of inheritance ->

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

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

Emperor.prototype = new Penguin();

var emperor = new Emperor("Napoleon");

console.log(emperor.numLegs);

#9

The properties may be inherited but they are not defined in this instance, as illustrated above. The Emperor constructor must define them. This contrasts with inheritance from a prototype in which all properties are clearly defined.

@factoradic, the example you give differs from the above in that you have defined numLegs in the Penguin constructor. That property is inherited as defined, as you suggest. Again, it is not the same as what we have here.


#10

I should be more specific.

What I was trying to show in my comment (and what @arcrockstar12762 had stated) is that your example does not make sense. Let me explain why.

You created two classes - Penguin and Emperor with exactly the same properties (DRY principle?). The only difference is that you have set property numLegs to 2 in the constructor of Emperor class. This code:

Emperor.prototype = new Penguin(); // notice no arguments

means that class Emperor inherits from class Penguin. The problem is that it does not have anything to inherit.

You can delete the above line, you can delete whole Penguin class and your code will work in exactly the same way like before.

That is why in the base code Penguin class looks like that:

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

to show that Emperor can inherit numLegs from Penguin class.

Do you see my point?


#11

I see your point, perfectly. Do you see mine? The example is written in the context of the OP so fudges around until it arrives at defined values. It makes perfect sense to define numLegs in the Penguin constructor and not bother setting it in the Emperor, which also has two legs. But as long as the Penguin constructor is taking a numLegs parameter, not defining it as a primitive means there is nothing for Emperor to inherit; ergo, the extra property, just as name is repeated, since it too is not set in the inheritance.

My whole reasoning is to make sure that all the properties are defined in their instance.


#12

I understand now, you assume that change of the base code was intentional, which is a complete opposite of my belief that it was the result of voodoo programming.

It is good to sometimes exchange views with a person who has a completely different approach. Thank you, it was interesting :smile:


#15

this worked!!!!

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 Penguin("sam");
console.log(emperor.numLegs);


#16

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("Princess");
console.log(emperor.numLegs);

I think the purpose of the assignment was to show that you can inherit properties from other places. In your code, you defined the variable 'emperor' as a new Penguin. I think you should have defined it as an 'Emperor' like I have done.
The Penguin object already had a set number of Legs. So, finding that value is really nothing special. We have been doing that for a while. I think instead of your line; var emperor=new Penguin("sam");
you should have typed; var emperor = new Emperor("sam");


#17

good to know i will remember that thank! :smile: