当我运行以下代码时,出现错误:TypeError: Object [object Object]
// create your Animal class here
function Animal(name, numLegs)
{
this.name = name;
this.numLegs = numLegs;
}
// create the sayName method for Animal
Animal.prototype = function sayName()
{
console.log("Hi, my name is [name]");
};
// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();
为什么?
最佳答案
我认为这就是问题所在
Animal.prototype = function sayName(){
console.log("Hi, my name is [name]");
};
应该是
Animal.prototype.sayName = function(){
console.log("Hi, my name is ", this.name);
};
此外,[name]
不是 javascript :S
Demo
关于javascript - codecademy的这段代码有什么错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17630542/