Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向类原型添加方法时出错_Javascript_Class_Methods - Fatal编程技术网

Javascript 向类原型添加方法时出错

Javascript 向类原型添加方法时出错,javascript,class,methods,Javascript,Class,Methods,关于向对象添加方法的快速问题 为什么我要找回错误? 我检查了语法,它似乎是正确的。 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 = functio

关于向对象添加方法的快速问题

为什么我要找回错误? 我检查了语法,它似乎是正确的。 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'
TypeError:对象#没有方法“sayName”

是的,因为您已经用小的
n
字母将方法声明为
sayname


JavaScript是一种区分大小写的语言。

是的,因为您已经用小的
n
字母将该方法声明为
sayname

JavaScript是区分大小写的语言。

您调用了
sayName()
,添加的函数是
function sayName(){}
您调用了
sayName()
,添加的函数是
function sayName(){}