【问题标题】:How to set enumerable property for Babel如何为 Babel 设置可枚举属性
【发布时间】:2017-01-13 22:11:41
【问题描述】:

我正在尝试在 TypeScript 项目中使用使用 Babel 编译的模块。但是我在静态属性和方法方面遇到了错误。

这是extends 的 TypeScript 助手。如您所见,它使用for..in

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

有没有办法为 Babel 的静态属性/方法设置 enumerable

【问题讨论】:

  • 您可以手动Object.defineProperty(XmlObject, 'LoadXml', {enumerable: true})。 ES6 规范规定这些是不可枚举的。
  • 你说你有一个错误,但不是错误是什么。根据 ES6 规范,静态方法不可枚举(参见 14.5.14 Runtime Semantics: ClassDefinitionEvaluation 下的 21.b.)。您的屏幕截图显示Test.LoadXml() 正确调用XmlObject.LoadXml(它记录“Success”)。你想完成什么?
  • @Jordan 我在 Test.LoadXml() 上没有结果。这是错误,因为它必须有这个功能
  • 在我看来应该修复 TypeScript 助手。

标签: javascript ecmascript-6 babeljs


【解决方案1】:

ES2015 classes transform插件中有一个宽松选项,默认值为false。您可以像这样将此值设置为true

{
  "plugins": [
    ["transform-es2015-classes", {
      "loose": true
    }]
  ]
}

使用此设置,方法将在类原型对象上定义。但这会导致一些其他问题。

class Foo {
  set bar() {
    throw new Error("foo!");
  }
}
class Bar extends Foo {
  bar() {
    // will throw an error when this method is defined
  }
}

更多详情请查看document

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 2020-01-21
    • 2014-09-11
    • 2013-05-09
    • 2015-05-08
    • 1970-01-01
    相关资源
    最近更新 更多