【发布时间】:2020-05-02 14:18:28
【问题描述】:
我不确定在构造函数中传递类本身是否合理,如下所示:
class OngoingActions {
constructor(Activation) {
this._actions = {
onTurnStart: [new Activation(), new Activation()],
onTurnEnd: [new Activation(), new Activation()],
onDraw: [new Activation(), new Activation()],
onMove: new Activation()
};
}
}
我了解,对于依赖注入,您需要传递该类的一个实例。在构造函数中传递同一个类的 7 个或更多实例会感觉有点尴尬。我也可以导入整个 '_actions' 对象,但是在同一个类中看到对象的结构感觉很好。处理这个问题的最佳方法是什么?
【问题讨论】:
标签: javascript class oop constructor