简体   繁体   English

子类的ES6访问功能

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM