向称为“说话”的原型添加方法

时间:2019-03-08 01:42:17

标签: javascript string function object ecmascript-6

继续获取错误:

应在原型中添加一个称为“语音”的方法

预计“ 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;
}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

return `${this.name}  ${this.speak} ${this.noise}`;
相关问题