Its All In The Genes


Really dont know why this keeps happening to me, but I think im not understanding a simple concept. Would love if someone could break it down to me.

Why does this code pass the question, but doesnt print anything to the console? Its question 16 in Objects II

function Animal(name, numLegs){ = name;
    this.numLegs = numLegs;

Animal.prototype.sayName = function(){console.log("Hi my name is " +

var penguin = new Animal("Captain Cook", 2);


Here's a breakdown of the code you gave us;

function Animal(name, numLegs):

This is how we create an object's "blueprint", with attributes, or parameters we want. In this case, we give our animal a name and a number of legs. We then set whatever object is created's attributes to what are provided by the user, when the object is actually created.

When we write Animal.prototype.sayName function, we are writing a function that can be used by only an object of the Animal class.

new Animal("Captain Cook", 2); serves to create the actual object, in this case, a penguin, and store it in the variable penguin. The variable penguin now references the created Animal object.

penguin.sayName(); runs the function we defined for the animal to say its name. When we call it on the penguin object, Captain Cook will tell us his name, like we told him to...

Well trained penguin, wouldn't you say?


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