【发布时间】:2013-04-05 07:36:15
【问题描述】:
关于向对象添加方法的快速问题。
为什么我会返回错误? 我检查了语法,它似乎是正确的。 Javascript 新手。
// 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);
};
// test
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();
我在尝试运行代码时收到此错误
TypeError: Object #<Animal> has no method 'sayName'
【问题讨论】:
标签: javascript class methods