B扩展A:在B中设置将覆盖get

时间:2015-12-11 18:25:56

标签: javascript typescript ecmascript-6 getter-setter

TypeScript:view on playground

yarn application -list

在编译的B类中,这将覆盖class A { protected _name: string = "" set name(name: string) { this._name = name } get name() { return this._name } } class B extends A { protected _name: string = "" set name(name: string) { this._name = name + "B" } } set的定义:

get

结果是,Object.defineProperty(B.prototype, "name", { set: function (name) { this._name = name + "B"; }, enumerable: true, configurable: true }); 在B类上不再起作用了:

get name

有没有办法从A类继承getter?

1 个答案:

答案 0 :(得分:1)

以下代码适用于TypeScript编译器,没有任何错误:

super.name

与@Crowder代码的唯一区别在于我使用super["name"]代替super.name。如果您使用Only public and protected methods of the base class are accessible via the 'super' keyword,编译器将发出此错误:super.name。请注意:TypeScript在发现错误时仍会编译,因此使用Dim Str As String = "XYZ" 也可以正常工作,尽管有错误。