It's All in the Genes (this concept)


#1

Here is the code which works. I am confused that how could we use 'this' keyword in the sayName method. From what I understand 'this' can be used only in context, for example within the parenthesis of the class constructor. Please explain.

// create your Animal class here
var Animal = function(name, numLegs){
this.name = name;
this.numLegs = numLegs;
};

// create the sayName method for Animal
Animal.prototype.sayName = function()
{
console.log("Hi my name is " + this.name);
};


#2

The sayName method is adding on to the class constructor. It gives all animals the property sayName, just like the regular constructor gives all animals the property numLegs.


#3

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