给定下面的代码,如何访问Parent
类中的bar()
?来自子类的ES6访问函数
class Parent{
constructor(){
}
foo(){
this.bar() // not defined
}
}
class Child extends Parent{
constructor(){
super();
}
bar(){
}
}
给定下面的代码,如何访问Parent
类中的bar()
?来自子类的ES6访问函数
class Parent{
constructor(){
}
foo(){
this.bar() // not defined
}
}
class Child extends Parent{
constructor(){
super();
}
bar(){
}
}
你做你确实做到了访问,但为了能在this
实际定义,你必须创建一个Child
实例不是Parent
之一:
var c = new Child();
c.bar() // works
c.foo() // calls c.bar() as well
'Parent'类不知道任何关于延伸它的“孩子”。 –
@DmitriPavlutin但这是...悲伤。反正谢谢:) – CENT1PEDE
值得一提的是,孩子知道所有关于父母 –