【发布时间】:2017-01-09 02:48:06
【问题描述】:
在我的库中(对于 AngularJS),我正在尝试实现以下功能:
- 你可以用一些装饰器标记一个空的类属性
- 在初始化过程中,这个属性被
Object.defineProperty定义的getter替换。 - 用户可以在类内部调用该属性,getter会被调用。
但是我在 Babel 中遇到了一个意想不到的行为。在库中使用的 Typescript 中,它运行良好,但是 Babel 创建了一些代码,将已经定义的属性重新定义为它的初始化程序。
是 ES2015 中的装饰类:
@Component({
selector: 'test'
})
export class TestClass {
@Inject('$http') $http;
@Inject('$q') $q;
}
有结果代码:
var TestClass = exports.TestClass = (_dec9 = (0, _ngMetasys.Component)({
selector: 'test'
}), _dec10 = (0, _ngMetasys.Inject)('$http'), _dec11 = (0, _ngMetasys.Inject)('$q'), _dec9(_class4 = (_class5 = function TestClass() {
(0, _classCallCheck3.default)(this, TestClass);
// everything is OK, TestClass.prototype.$http is a getter with function () => $http.
_initDefineProp(this, '$http', _descriptor5, this); // there are dragons. Property $http is redefined to undefined.
_initDefineProp(this, '$q', _descriptor6, this);
}, (_descriptor5 = _applyDecoratedDescriptor(_class5.prototype, '$http', [_dec10], {
enumerable: true,
initializer: null
}), _descriptor6 = _applyDecoratedDescriptor(_class5.prototype, '$q', [_dec11], {
enumerable: true,
initializer: null
})), _class5)) || _class4);
如何禁用此属性重新定义?有没有办法避免 babel 的属性重新定义或替代它?
【问题讨论】:
标签: javascript decorator babeljs