【发布时间】:2021-02-26 21:26:45
【问题描述】:
我想这样做:
function call(method) {
const object = // Retrieve the class that declares the method and then construct an instance dynamically.
method.call(object) // So 'this' is bound to the instance.
}
call(MyClass.prototype.myMethod)
call(AnOtherClass.prototype.anOtherMethod)
我的目标是从依赖容器创建实例。这就是为什么该方法必须绑定到一个实例,该实例将具有该类所需的所有依赖项。
【问题讨论】:
-
是否也传入对象?
-
与其他具有
class系统的语言不同,在 JavaScript 中,方法和声明它们的类之间没有永久关系。 -
是的,我将传递类对象以及@CertainPerformance 提到的方法
标签: javascript class reflection introspection