Javascript equivalent of php call_user_func

I found this thread that I implemented (see the accepted answer):
javascript equivalent to PHP call call_user_func ()

However, I have a problem with multiple parameters. I understand what I am doing by turning my parameters into strings and treating them as 1 parameter, but I don't know how to fix this because I am creating parameters dynamically.

Meaning, I have defined the following in my code:

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 called 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 need to use arrays for this. I don't understand JS OOP (haven't tried it yet), although I like PHP OOP, so I don't know if there is a way to do it there.

Any help passing multiple parameters would be appreciated.

Thank.

+3


source to share


2 answers


The first thing to do is clean up all the code, start over. Your approach will not get you where you would like to be. (Unfortunately, I cannot tell you where you want to be, because I cannot understand your example.)

There are three ways to call a function in JavaScript.

function foo() { console.log(arguments); }

// 1. directly
foo(1, 2, 3);

// 2. trough Function.call()
foo.call(this, 1, 2, 3);

// 3. trough Function.apply()
var args = [1, 2, 3];
foo.apply(this, args);

      

call

and apply

similar. They allow you to decide which object the keyword this

within the function will point to (this is the important bit!).

apply

takes an array of arguments, call

takes individual arguments.

The closest thing to call()

is PHP call_user_func()

. The closest thing to apply()

is PHP call_user_func_array()

.

JavaScript objects share something with PHP arrays: they are key / value pairs.

// an anonymous function assigned to the key "foo"
var obj = {
  foo: function () { console.log(arguments); }
};

      



This means that you can access the properties of an object using dot notation:

// direct function call
obj.foo(1, 2, 3);

      

Or through the notation in square brackets (note that object keys are strings):

var funcName = "foo";
obj[funcName](1, 2, 3);
obj[funcName].call(obj, 1, 2, 3);
obj[funcName].apply(obj, [1, 2, 3]);

      

The bracketed notation gives you the ability to dynamically select an object property. If this property is a function, apply()

it gives you the freedom to dynamically select the function arguments.

Every top-level function that has not been declared as a property of any object will become a property of the global object. In browsers, the global object is window

. (So function foo()

in my first code block above is this window.foo

.)

Please note that this

does not work as in PHP. It will point to the object to which the function was called, not to the object to which the function "belongs". (The concept of "belongs" doesn't really exist in JavaScript. Things can be modeled this way, but it's just a convention.)

When called directly, ( obj.foo(1, 2, 3)

) this

will point to obj

. C call

and apply

, this

will point to whatever object you want. This is much more useful than meets the eye. Most of the time, when you want to call functions dynamically, you will end up using apply

.

+13


source


Check out Function.apply:



function test(a, b) { console.log([a, b]) }

test.apply(null, [1, 2]); // => [ 1, 2 ]

      

+1


source







All Articles