[英]ES6 access functions from child class
Given the codes below, how do I access the bar()
from an the Parent
class? 鉴于以下代码,如何从
Parent
类访问bar()
?
class Parent{
constructor(){
}
foo(){
this.bar() // not defined
}
}
class Child extends Parent{
constructor(){
super();
}
bar(){
}
}
You do access it exactly as you did, but for it to be actually defined on this
you have to create a Child
instance not a Parent
one: 你做你确实做到了访问,但为了能在实际界定
this
你必须创建一个Child
实例而不是Parent
之一:
var c = new Child();
c.bar() // works
c.foo() // calls c.bar() as well
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.