typescript中的类内循环原型在es2015中的行为不同
当我将typescript编译到es5时,这段代码对我来说很好typescript中的类内循环原型在es2015中的行为不同,typescript,ecmascript-6,prototype,Typescript,Ecmascript 6,Prototype,当我将typescript编译到es5时,这段代码对我来说很好 export class MyClass { public static n: MyClass = <MyClass>{}; constructor() { for (let name in MyClass.prototype) { if (MyClass.hasOwnProperty(name)) { MyClass.n[this
export class MyClass {
public static n: MyClass = <MyClass>{};
constructor() {
for (let name in MyClass.prototype) {
if (MyClass.hasOwnProperty(name)) {
MyClass.n[this[name]] = `${this.constructor.name}.${name}`;
}
}
}
SomeFunction() {
return true;
}
}
导出类MyClass{
公共静态n:MyClass={};
构造函数(){
for(在MyClass.prototype中输入名称){
if(MyClass.hasOwnProperty(名称)){
MyClass.n[this[name]=`${this.constructor.name}.${name}`;
}
}
}
SomeFunction(){
返回true;
}
}
但是如果我试图编译到一个更新的版本(es2015、es2016、es2017),这段代码会跳过for循环,就好像
DataActions.prototype
是空的一样。我很难找到是什么改变了这个密码。有更好的方法吗?可以检查编译后的js输出吗?你可能会在那里找到答案。此外,您可能还发现了一个bug?在编译@BalázsÉdes时,没有任何东西真正突出。到了es5,它被转换成了一个函数,而在es2015,它确实使用了新的js类。这两个版本的原型似乎有所不同MyClass.prototype
是es2015是空的,而es5有SomeFunction
我理解。我在想你是在进一步编译ts文件的输出,通常是这样编译的:ts->es6->babel->es5.es6类方法是不可枚举的,Typescript在编译到es5时不会实现这种行为。你能检查编译后的js输出吗?你可能会在那里找到答案。此外,您可能还发现了一个bug?在编译@BalázsÉdes时,没有任何东西真正突出。到了es5,它被转换成了一个函数,而在es2015,它确实使用了新的js类。这两个版本的原型似乎有所不同MyClass.prototype
是es2015是空的,而es5有SomeFunction
我理解。我认为您正在进一步编译ts文件的输出,通常的编译方式是:ts->es6->babel->es5.es6类方法是不可枚举的,而Typescript在编译到es5时不会实现这种行为。