Javascript 匿名函数还返回js中未定义的值

Javascript 匿名函数还返回js中未定义的值,javascript,javascript-objects,Javascript,Javascript Objects,我是JS新手,我正在学习雄辩的JS。我已经知道,像我编写的重写toString属性的匿名函数会立即执行。但我不明白的是为什么它太晚才返回未定义 class Rabbit{ constructor(type){ this.type = type; } speak(line){ console.log("hello"); } } let killerRAbbit = new Rabbit("killer"); Rabbit

我是JS新手,我正在学习雄辩的JS。我已经知道,像我编写的重写toString属性的匿名函数会立即执行。但我不明白的是为什么它太晚才返回未定义

class Rabbit{ 
  constructor(type){
    this.type = type;
  }
  speak(line){
    console.log("hello");
  }
}

let killerRAbbit = new Rabbit("killer");

Rabbit.prototype.toString = function() {
  return `${this.type}, ${this.speak()}`;
};
console.log(killerRAbbit.toString());
我在firefox浏览器中执行了这段代码,得到的结果是

hello
killer, undefined

有人能解释一下吗?

speak方法不返回值,因此默认情况下它返回
未定义的
。您将获取其返回值并将其放入字符串中。你期望什么样的输出?是的,我现在看到了。我希望输出是“杀手,你好”。对不起,先生。我想我应该给自己更多的时间来调试它。非常感谢您,Felix Kling先生。