21/30 Up the Food-I-mean-Prototype Chain


#1

can anyone help??

// original classes
function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
    this.isAlive = true;
}
function Penguin(name) {
    this.name = name;
    this.numLegs = 2;
}
function Emperor(name) {
    this.name = name;
    this.saying = "Waddle waddle";
}

// set up the prototype chain
Penguin.prototype = new Animal();
Emperor.prototype = new Penguin();

var myEmperor = new Emperor("Jules");

console.log(Emperor.saying); // should print "Waddle waddle"
console.log(Penguin.numLegs); // should print 2
console.log(Animal.isAlive); // should print true

#2

Next time please be more clear about what your issue is.

But basically, in your console log you should be saying

console.log(myEmperor.whateverMethod);

Reason being is this is all about inheritance. Or the prototype chain, etc. myEmperor inherits all the methods from the constructors so myEmperor is the one you want to use. myEmperor.saying, etc. Similarly, using any of the initial constructors wouldn't work because they weren't initialized (and shouldn't be). They're just scalfolding.

Hope that helps.


#3

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