Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
typescript中的类内循环原型在es2015中的行为不同_Typescript_Ecmascript 6_Prototype - Fatal编程技术网

typescript中的类内循环原型在es2015中的行为不同

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

当我将typescript编译到es5时,这段代码对我来说很好

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时不会实现这种行为。