I’ve found this topic which I’ve implemented (see accepted answer):
javascript equivalent of PHP's call_user_func()
However, I am having a problem with multiple parameters. I realize what I was doing was turning my parameters into strings and treating it like 1 parameter, but I don’t know how to fix this because I am dynamically creating the parameters.
Meaning, I have defined in my code the following:
var a = new Array();
a[0] = new Array();
a[0][0] = 'alert';
a[0][1] = '\'Hello World\'';
a[1] = new Array();
a[1][0] = 'setTimeout';
a[1][1] = 'alert("goodbye world")';
a[1][2] = '20';
Later, I was calling them like this:
var j = 0;
var len = 0;
var fx = '';
var params = '';
for( i in a ){
params = '';
len = a[i].length;
fx = a[i][0]; // getting the function name
a[i].splice( 0, 1 ); // removing it from array
if( len > 1 ){
params = a[i].join(", "); // trying to turn the parameters into the right format, but this is turning it into strings I think
params = params.replace(/\\'/g,'\''); // bc i was adding slashes with PHP
}
window[fx](params);
}
I don’t have to use arrays to do this. I don’t understand JS OOP (haven’t tried yet), though I am comfortable with PHP OOP, so I don’t know if there is a way to do this there.
Any help on passing multiple parameters would be appreciated.
Thanks.
First thing to do: Scrap your entire code, start over. Your approach will not get you anywhere where you’d want to be. (Unfortunately I can’t tell you where you’d want to be because I cannot make sense of your example.)
There are three ways to call a function in JavaScript.
callandapplyare similar. They let you decide which object thethiskeyword will point to inside the function (that’s the important bit!).applyaccepts an array of arguments,callaccepts individual arguments.The closest thing to
call()is PHP’scall_user_func(). The closest thing toapply()is PHP’scall_user_func_array().JavaScript objects share something with PHP arrays: They are key/value pairs.
This means you can access object properties either with the dot notation:
Or through square bracket notation (note that object keys are strings):
Square bracket notation gives you the freedom to choose an object property dynamically. If this property happens to be a function,
apply()gives you the freedom to choose function arguments dynamically.Every top-level function that has not been declared as the property of some object will become the property of the global object. In browsers the global object is
window. (So thefunction foo()in my first code block above really iswindow.foo.)Note that
thisdoes not work like in PHP. It will point to the object the function has been called on, not the object the function “belongs to”. (The concept “belongs to” does not really exist in JavaScript. Things can be modeled that way, but it’s only a convention.)With direct calling (
obj.foo(1, 2, 3)),thiswill point toobj. Withcallandapply,thiswill point to whatever object you want to. This is a lot more useful than it sounds at first. Most of the time when you want to call functions dynamically, you will end up usingapply.