相当于PHP的call_user_func() [英] javascript equivalent of PHP's call_user_func()
问题描述
我想使用变量名称调用一个函数。
<
编辑:
我在这里发布了一个小提琴,我试着去做:
http://jsfiddle.net/sAzPA/
< div id =some>
...
< / div>
js:
< code $(函数($){
$ .fn.MyPlugin = function(){
return this.each(function(){
var somefunction = function (arg1,arg2){alert(arg1);},
someotherfunction = function(arg1,arg2){alert(arg2);},
reallyimportantfunction = function(arg1,arg2){
var foo = $(this).attr('id')+'function';
//这里我想用foo值作为名称来调用函数,并将其传递给arg1和arg2
$ b $()[foo](arg1,arg2); //< - does not work
};
reallyimportantfunction();
});
};
})(jQuery);
$ b jQuery(函数($){
$('#some')。MyPlugin();
});
如果函数是在全局级定义的,那么它会自动成为窗口
对象的子元素。
因此,您可以始终调用窗口.functionName();
您通常只需调用 functionName();
。
<此外,由于Javascript对象像关联数组一样工作,因此可以使用如下的数组语法来调用任何对象的任何子对象: object ['childName']
。这包括函数,所以你可以为任何属于对象成员的函数执行 object ['functionName']();
将这两点结合在一起,您可以调用任何全局定义的函数,如下所示:
$ b
window ['functionName ]();
由于上面例子中的 functionName
是一个字符串,你可以在这些括号中使用一个变量,这意味着你的功能与PHP的 call_user_func()
。
正如我所说,这适用于任何对象。 OP的评论指出,他希望以这种方式使用的功能位于JQuery插件中。因此它们可能是JQuery对象的一部分,并且通常会这样调用: JQuery()。functionName();
(或者与 $
代替 JQuery
)。
JavaScript语法允许我们使用<在任何我们可以使用 .functionName()
的地方使用code> ['functionName']() JQuery示例中,我们可以将其更改为如下所示:
JQuery()['functionName']();`
但是这种技术可以适用于任何Javascript对象。任何你使用 .functionName()
的地方都可以用 ['functionName']()
/ p>
Does anyone know if there is one?
I want to call a function using a variable name.
edit:
I posted a fiddle here with what I'm trying to do:
http://jsfiddle.net/sAzPA/
<div id="some">
...
</div>
js:
(function($){
$.fn.MyPlugin = function(){
return this.each(function(){
var somefunction = function(arg1, arg2){ alert(arg1); },
someotherfunction = function(arg1, arg2){ alert(arg2); },
reallyimportantfunction = function(arg1, arg2){
var foo = $(this).attr('id') + 'function';
// here I want to call the function with the foo value as name, and pass it arg1 and arg2
$()[foo](arg1, arg2); // <- doesn't work
};
reallyimportantfunction();
});
};
})(jQuery);
jQuery(function($){
$('#some').MyPlugin ();
});
If a function is defined at the global level, then it automatically becomes a child of the window
object.
Therefore you can always call window.functionName();
any place you would normally just call functionName();
.
Further, since in Javascript objects work like associative arrays, you can call any child of any object using array syntax like this: object['childName']
. This includes functions, so you can do object['functionName']();
for any function which is a member of an object.
Combining these two points together, you can call any globally defined function like so:
window['functionName']();
And since functionName
in the above example is a string, you can use a variable in those brackets, which means you've got the same functionality as PHP's call_user_func()
.
[EDIT]
As I stated, this works for any object. The OP's comments state that the functions he wants to use this way are in a JQuery plug-in. They are therefore likely to be part of the JQuery object, and would normally be called like so: JQuery().functionName();
(or with the $
in place of JQuery
).
Javascript syntax allows us to use ['functionName']()
in any place where we can use .functionName()
, so therefore, taking the above JQuery example, we could change it to look like this:
JQuery()['functionName']();`
But this technique can be adapted for any Javascript object. Any place where you use .functionName()
, it can be replaced with ['functionName']()
.
这篇关于相当于PHP的call_user_func()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!