为什么打印出来的是“bye”而不是“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()
最佳答案
您正在重写“原型(prototype)”上的 hello 定义。 当你做 class Test () ... hello 相当于
测试.prototype.hello
类语法主要是函数的正常原型(prototype)定义之上的糖分。
关于Javascript 原型(prototype)继承不调用预期的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44013672/