gpt4 book ai didi

typescript - TypeScript 编译器生成的新版本的 __extends

转载 作者:搜寻专家 更新时间:2023-10-30 21:25:05 25 4
gpt4 key购买 nike

当您使用继承时,TypeScript 编译器会为您生成 __extends 函数。旧版本的 tsc 编译器生成类似这样的东西

var __extends = this.__extends || function (d, b) {
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};

设置b的一个实例作为d的原型(prototype)链。这也差不多是我想要手动完成的。

最新版本 (0.9) 添加了属性/方法引用的复制,这对我来说看起来是多余的:

var __extends = this.__extends || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};

有人知道这是什么原因吗?

最佳答案

它现在也尊重类的静态属性。

关键语句是:

for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];

这会将父类静态成员复制到子类。

例如:

class Foo{
static x = "asdf";
}

class Bar extends Foo{

}

alert(Bar.x);

Try it

原始(现已关闭)错误报告:http://typescript.codeplex.com/workitem/825

关于typescript - TypeScript 编译器生成的新版本的 __extends,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com