一个低调的例子是这个类,继承自PublishableAction
(提供静态.viewController()
函数以及// ...
所在位置使用的其他方法):
class Action extends PublishableAction {
static actionSelector = {
buttonCommit: Symbol()
};
// This part still works as intended, accessing 'Action':
static buttonSession = Action.viewController({
perform: Action.actionSelector.buttonCommit
});
// ... but here's the issue:
// ReferenceError: Cannot access 'Action' before initialization
[Action.actionSelector.buttomCommit]() {
// ...
}
}
我已经尝试使用[this.actionSelector.buttonCommit]()
和[this.constructor.actionSelector.buttonCommit]()
,两者对结果没有任何影响。
如何修复ReferenceError
而不必将Action.actionSelector
移动到全局范围?
您试图实现的模式有点奇怪。但也许有一个"方法"getter和返回每个可调用的方法依赖于actionSelector?
class Action extends PublishableAction {
static actionSelector = {
buttonCommit: Symbol()
};
// This part still works as intended, accessing 'Action':
static buttonSession = Action.viewController({
perform: Action.actionSelector.buttonCommit
});
get methods() {
return {
[Action.actionSelector.buttonCommit]: function() {
//...
}
};
}
}
可以调用(new Action()).methods[Action.actionSelector.buttonCommit]()