Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript equivalent of PHP's call_user_func()

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 ();

});
like image 708
Alex Avatar asked Apr 28 '11 12:04

Alex


3 Answers

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']().

like image 121
Spudley Avatar answered Nov 10 '22 04:11

Spudley


There are quite a few ways of achieving this. The quickest and the dirties (and unsafe!) way is to do (PS:fnName in the below examples is the name of the function stored as string)

eval(fnName)

However you can also do

            this[fnName]();//careful with "this" though. Try replacing with "window" if it doesnt work for you

or to call it by passing arguments

this[fnName].apply(contextObject,argumentArray) //contextObject will be objest which will be referenced by the keyword "this" within the function body (fnName's body), argumentArrayy is the array of arguments you want to pass to function.
like image 20
Nikhil Avatar answered Nov 10 '22 04:11

Nikhil


var a = function(foo) {console.log(foo);}

a.call(null, 'test');
a('test');
like image 23
Emmerman Avatar answered Nov 10 '22 03:11

Emmerman