3

I'm trying to pass arbitrary number of arguments to function. The arguments should be json type [function : arrayOfArgs]. The keys are functions and the values are arrays of the arguments, that should be passed to those functions.

At first, I considered function with only a number of argument

 function _gl(f,args){ f.apply(null,args); }

 function func(a,b){ alert(a+b); }

//calling the function

<input type="button" value="test" onclick="_gl(func,['2','3']);"/>

and it works pretty good .Now I'm trying to generalize that method

function _GL(){
      var arguments = _GL.arguments; 

      var i=0;
      for(i;i<arguments.length;i++)
      {
        var A=arguments[i];
        for(j in A) j.apply(null,A[j]); 
      } 
   }

//and calling it

<input type="button" value="TEST" onclick="_GL({func:['2','3']});"/>

but i'm getting the following error "Uncaught TypeError: Object func has no method 'apply' ".

3
  • func you pass is the Event object, it's not a function, and why should it? Commented Sep 25, 2013 at 13:45
  • You're passing the array ['2', '3'] to the attribute func. Commented Sep 25, 2013 at 13:47
  • should be json type... JSON is a string representation of data. Your array is not "JSON type". It's just a "normal" JavaScript array. If it was JSON, it's be a string. If it's not a string, it's not JSON. Commented Sep 25, 2013 at 13:50

3 Answers 3

1

You can use this code jsFiddle:

_GL = function() {
    var arguments = _GL.arguments;
    var i = 0;
    for (i = 0; i < arguments.length; i++) {
        var arg = arguments[i];
        for (j in arg) {
            var f = window[j];
            f.apply(null, arg[j]);
        }
    }
};

As you can see you have to get your function f first from the window element by its name. Then f has the right type an args can be applied.

Sign up to request clarification or add additional context in comments.

Comments

1
{func:['2','3']}

You are making an object with a (string) key called "func" with value ['2','3']. Strings are not functions, so it doesn't have .apply().

In objects, your keys must be strings, you cannot use other types as keys.


To "generalize" it, you should pass it an array of functions and their arguments. Something like this:

[[func, ['2','3']], [func2, ['abc']]

So, if you did this:

onclick="_GL([[func, ['2','3']], [func2, ['abc']]);"

Then you could loop through and get the functions and call 'em.

function _GL(funcs){
    for(var i=0, len=funcs.length; i < len; i++){
        var func = funcs[i];
        func[0].apply(null, func[1]);
    }
}

2 Comments

Thank you RockeT Hazmat for your answer. What can I do to make that function work?
@Paramore: I edited my answer and gave you a possible solution.
1

One possible solution;

var _gl = function (callables) {

    // Loop through each property in the passed in object
    for (var fnName in callables) {

        // Only apply for properties that is local to `callables`
        if (callables.hasOwnProperty(fnName)) {
            window[fnName].apply(callables[property]);
        }

    }

};

... onclick='_gl({"MyFunction": ["a", 1, []]});' ...

Instead of using the global namespace, you could (and should!) set up an object with your callable functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.