为什么打印出“再见”而不是“你好”?根据此博客文章中描述的继承链,我以为它将记录为“ hello”。

http://sporto.github.io/blog/2013/02/22/a-plain-english-guide-to-javascript-prototypes/

class Test {
  hello() {
    console.log('hello')
  }
}

Test.prototype.hello = function(){
  console.log('bye')
}

const t = new Test
t.hello()

最佳答案

您正在“原型”上重写hello的定义。
当你做类Test()时……你好相当于

Test.prototype.hello

在函数的常规原型定义之上,类语法主要是糖。

关于javascript - Javascript原型(prototype)继承未调用预期方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44013672/

10-11 14:16
查看更多