因此,我想使用一个字符串className和一个字符串方法名通过反射来调用ES6静态类方法。我试过几种方法。不幸的是,我似乎找不到正确的方法来做到这一点。
顺便说一句(正如下面的评论中提到的),我正在寻找一个解决方案,我将从dom属性中获取类名和方法名,因此它们需要是一个字符串。
有人能帮上忙吗?
class a{
static b(nr){
alert('and the answer is' + nr)
}
}
let aClassName = 'a',
aMethodeName = 'b',
theCompleteCall = 'a.b',
anArgument = 42;
//Reflect.apply(theCompleteCall,anArgument);
//window[aClassName][aMethodeName](anArgument);
//window[theCompleteCall](anArgument);
发布于 2018-02-01 17:15:29
由于let和class没有像您期望的那样在全局作用域(read more)中声明,您需要在可访问的作用域中声明您的类,如下所示:
window.a = class a{
static b(nr){
alert('and the answer is' + nr)
}
}
let aClassName = 'a',
aMethodeName = 'b',
theCompleteCall = 'a.b',
anArgument = 42;然后,您可以使用反射进行调用,如下所示:
window[aClassName][aMethodeName](anArgument)
因此,解决方案是在声明它们时提供一个作用域,并通过该作用域访问它们。
发布于 2018-02-01 17:10:47
您将变量设置为字符串,而不是对对象的引用。
https://stackoverflow.com/questions/48558782
复制相似问题