Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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 codeacademy的代码中有什么错误_Javascript - Fatal编程技术网

Javascript codeacademy的代码中有什么错误

Javascript codeacademy的代码中有什么错误,javascript,Javascript,当我运行以下代码时,我得到一个错误: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() {

当我运行以下代码时,我得到一个错误:
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 = {
  sayName: function() {
    console.log("Hi, my name is " + this.name);
  }
};
将原型设置为函数并不是完全错误的,但问题是代码打算在原型对象上使用名为“sayName”的属性。提供一个名为“sayName”的函数不能用于该目的;该名称不会作为函数对象的属性公开


还请注意,只需在登录到控制台的字符串中输入“[名称]”不会导致记录动物名称。您必须显式地将它从对象的“name”属性修补到字符串中,就像我发布的代码一样。

我认为这就是问题所在

   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


你会得到什么错误?TypeError:Object[Object Object]没有方法“sayName”,它真的是直接从Codeacademy课程中复制出来的吗?你应该把它是错误的这一事实告诉别人。这是你必须找到错误的地方吗?或者其他一些是正确的,但请注意,动物名称不会被记录。没有注意到这一部分,谢谢:)