【问题标题】:TypeScript class: statics and inheritanceTypeScript 类:静态和继承
【发布时间】:2015-01-04 12:06:01
【问题描述】:

问题:

  1. 我观察到的行为是 TypeScript 的预期行为吗?
  2. 我观察到的行为是 ECMAScript 6 的预期行为吗?
  3. 是否有一种简单的方法可以返回继承层次结构来处理每个级别的“myStatic”数组?我怎么知道什么时候停止?

描述:使用 TypeScript 时,派生类和静态属性似乎有一些有趣的行为。

TypeScript Example

class MyBase {
    static myStatic = [];
}

class MyDerived extends MyBase {}

MyBase.myStatic = ['one', 'two', 'three']

class MyDerived2 extends MyBase {}

document.body.innerHTML += "<b>MyDerived.myStatic:</b>&nbsp;" + JSON.stringify(MyDerived.myStatic) + "<br/>";
document.body.innerHTML += "<b>MyDerived2.myStatic:</b>&nbsp;" + JSON.stringify(MyDerived2.myStatic) + "<br/>";

这是结果:

MyDerived.myStatic: []
MyDerived2.myStatic: ["one","two","three"]

编辑:添加说明 TypeScript 和 ECMA Script 6 之间不同行为的示例。注意:ECMA 脚本不支持静态属性,因此这些示例使用静态方法。

TypeScript 代码:

class MyBase {
  static myStatic() { return []; }
}

class MyDerived extends MyBase {}

MyBase.myStatic = () => { return ['one', 'two', 'three']; }

class MyDerived2 extends MyBase {}

document.body.innerHTML += "<b>MyDerived.myStatic:</b>&nbsp;" + JSON.stringify(MyDerived.myStatic()) + "<br/>";
document.body.innerHTML += "<b>MyDerived2.myStatic:</b>&nbsp;" + JSON.stringify(MyDerived2.myStatic()) + "<br/>";

TypeScript 结果:

MyDerived.myStatic: []
MyDerived2.myStatic: ["one","two","three"]

ECMA 脚本 6 代码:ES6 Fiddle

class MyBase {
  static myStatic() { return []; }
}

class MyDerived extends MyBase {}

MyBase.myStatic = () => { return ['one', 'two', 'three']; };

class MyDerived2 extends MyBase {}

console.log("MyDerived.myStatic: " + JSON.stringify(MyDerived.myStatic()));
console.log("MyDerived2.myStatic: " + JSON.stringify(MyDerived2.myStatic()));

ECMA 脚本 6 结果

MyDerived.myStatic: ["one","two","three"]
MyDerived2.myStatic: ["one","two","three"]

【问题讨论】:

    标签: static typescript ecmascript-6


    【解决方案1】:

    我观察到的行为是 TypeScript 的预期行为吗? 我观察到的行为是 ECMA 脚本 6 的预期行为吗?

    是的,是的。类允许运行时修改并按定义顺序处理。 inherit 仅在定义点起作用,因此取决于 在该点的基础属性。

    【讨论】:

      【解决方案2】:

      See related TypeScript bug report

      以下是具有一致行为并遍历继承层次结构的 TypeScript 和 ES6 代码示例:

      TypeScript 代码:

      class MyBase {
        static myStatic() { return []; }
      }
      
      class MyDerived extends MyBase {
        static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
      }
      
      MyBase.myStatic = () => { return ['one', 'two', 'three']; };
      
      class MyDerived2 extends MyBase {
        static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
      }
      
      document.body.innerHTML += ("MyDerived.myStatic: " + JSON.stringify(MyDerived.myStatic()) + "<br/>");
      document.body.innerHTML += ("MyDerived2.myStatic: " + JSON.stringify(MyDerived2.myStatic()) + "<br/>");
      

      TypeScript 结果:

      MyDerived.myStatic: ["mickey","goofy",["one","two","three"]]
      MyDerived2.myStatic: ["mickey","goofy",["one","two","three"]]
      

      ES6 代码:ES6 Fiddle

      class MyBase {
        static myStatic() { return []; }
      }
      
      class MyDerived extends MyBase {
        static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
      }
      
      MyBase.myStatic = () => { return ['one', 'two', 'three']; };
      
      class MyDerived2 extends MyBase {
        static myStatic() { return ['mickey', 'goofy', super.myStatic()]; }
      }
      
      console.log("MyDerived.myStatic: " + JSON.stringify(MyDerived.myStatic()));
      console.log("MyDerived2.myStatic: " + JSON.stringify(MyDerived2.myStatic()));
      

      ES6 结果

      MyDerived.myStatic: ["mickey","goofy",["one","two","three"]]
      MyDerived2.myStatic: ["mickey","goofy",["one","two","three"]]
      

      【讨论】:

        猜你喜欢
        • 2016-10-10
        • 2014-02-26
        • 2017-06-22
        • 2022-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-16
        相关资源
        最近更新 更多