Error: this is not a function


#1

Hello there!

My code below:

// create your Animal class here
function Animal (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);
};
Animal.prototype.numLegs = function () {
console.log("My numlegs is "+ this.numLegs);
};

// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();
penguin.numLegs();

I receive this message and I can't solve it :slight_smile:
TypeError: penguin.numLegs is not a function


#2

this.numLegs = numLegs So I think there is a conflict with a prototype called numLegs. Try changing your Animal.prototype.numLegs to Animal.prototype.sayLegs. Also change your call to the function accordingly.


#3

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