Inheritance 如何访问Typescript中基类的属性?

Inheritance 如何访问Typescript中基类的属性?,inheritance,properties,typescript,superclass,Inheritance,Properties,Typescript,Superclass,有人建议使用这样的代码 class A { // Setting this to private will cause class B to have a compile error public x: string = 'a'; } class B extends A { constructor(){super();} method():string { return super.x; } } var b:B = new B(); a

有人建议使用这样的代码

class A {
    // Setting this to private will cause class B to have a compile error
    public x: string = 'a';
}

class B extends A {
    constructor(){super();}
    method():string {
        return super.x;
    }
}

var b:B = new B();
alert(b.method());
它甚至获得了9票。但是当你把它贴在官方的TS操场上 它给你带来了错误


如何从B访问A的x属性?

使用
this
而不是
super

class A {
    // Setting this to private will cause class B to have a compile error
    public x: string = 'a';
}

class B extends A {
    // constructor(){super();}
    method():string {
        return this.x;
    }
}

var b:B = new B();
alert(b.method());

冠军!抱歉,没有足够的声誉+1