调用名称在变量中的 javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6871165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:09:41  来源:igfitidea点击:

Call javascript function which name is in variable

javascriptfunctionvariablescall

提问by user769154

The problem is next: Assume that we have a select. Onchange event we need to call some function, but name of this function kept in variable. How to call this function ?

接下来的问题是:假设我们有一个选择。Onchange 事件我们需要调用一些函数,但这个函数的名称保持在变量中。如何调用这个函数?

回答by Raynos

window[name]()

You can call functions by name reference by selecting them as a property of windowand executing them

您可以通过选择它们作为属性window并执行它们来按名称引用调用函数

回答by Pwaddles

have you tried variableName();?

你试过variableName();吗?

We often pass around callback functions, which might look something like this

我们经常传递回调函数,它可能看起来像这样

function doSomething(callbackFunction){
    // some happy code
    callbackFunction()
}