繁体   English   中英

在原型中添加一种称为“说话”的方法

[英]Adding a method to a prototype called 'speak'

不断收到错误:

应该在原型中添加一个方法,称为语音

预期“ DogsaysWoof”为“ Dog said Woof”。

以为我钉了钉子,但是缺少了一些东西。 我在属性之间放置了空格“”,但仍然出现“ DogsaysWoof”。 认为这是因为我缺少对原型的方法的引用,但是似乎放在这里并不重要。 (现在是“说”)

我对此有些慌张。

function exerciseTwo(AnimalClass){
  // Exercise Two: In this exercise you are given a class called AnimalClass.
  // The class will already have the properties 'name', 'noise' on it.
  // You will be adding a method to the prototype called 'speak'
  // Using the 'this' keyword, speak should return the following string:
  // '<name> says <noise>'
  // DO NOT create a new class or object

  /*My ************************************************************** Code*/
  AnimalClass.prototype.speak = function(says){
    this.speak = 'says';
    return this.name + '' + this.speak + '' + this.noise;
  };
  // Please write your code in the lines above
  return AnimalClass;
}

您实际上需要添加空格-当前它们是空字符串:

return this.name + ' ' + this.speak + ' ' + this.noise;
//                  ^                  ^

您可以使用Template文字编写这样的文字。 您无需担心级联。

return `${this.name}  ${this.speak} ${this.noise}`;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM