TypeScript中受保护的等价物是什么?

Tah*_*khi 46 typescript

什么是相当于保护打字稿

我需要在基类中添加一些成员变量,仅用于派生类.

Fen*_*ton 49

更新

2014年11月12日.TypeScript 1.3版可用,包含protected关键字.

2014年9月26日.protected关键字已登陆.它目前正在发布.如果您使用的是非常新版本的TypeScript,现在可以使用protected关键字...以下答案适用于旧版本的TypeScript.请享用.

查看protected关键字的发行说明

class A {
    protected x: string = 'a';
}

class B extends A {
    method() {
        return this.x;
    }
}
Run Code Online (Sandbox Code Playgroud)

老答案

TypeScript只有private- 不受保护,这只在编译时检查时表示私有.

如果你想访问super.property它必须是公开的.

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

class B extends A {
    method() {
        return super.x;
    }
}
Run Code Online (Sandbox Code Playgroud)