19. B/W Peguin Magic (side experiment)


#1


(Note: The below code was used to practice the concepts employed in this lesson. Obviously, it is not the code offered and suggested in the lesson.)

I would have thought that the name property from the Animal class would have been brought over by the prototype containing statement:

Bird.prototype = new Animal();

just as the numLegs properties were for both classes: Bird and Human but the name property apparently was not.

I'm puzzled.


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

function Bird(name) {
};

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

Bird.prototype = new Animal();
Human.prototype = new Animal();

var robin = new Bird("Phil");
var man = new Human("Bob");

console.log(robin.name); // output: undefined
console.log(robin.numLegs); // output: 2
console.log(man.name); // output: Bob
console.log(man.numLegs); // output: 2


#2

@jnl2,
Try a google search
javascript can i set property in prototype chain site:stackoverflow.com
for instance
http://stackoverflow.com/questions/572897/how-does-javascript-prototype-work


#3

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