当我运行以下代码时,我收到一个错误: 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();
Run Code Online (Sandbox Code Playgroud)
为什么?
我认为这是问题所在
Animal.prototype = function sayName(){
console.log("Hi, my name is [name]");
};
Run Code Online (Sandbox Code Playgroud)
应该
Animal.prototype.sayName = function(){
console.log("Hi, my name is ", this.name);
};
Run Code Online (Sandbox Code Playgroud)
另外[name]是不是JavaScript的:S
| 归档时间: |
|
| 查看次数: |
322 次 |
| 最近记录: |