There is one function from which you need to call the one passed in the arguments:

function testone(a) {} testone(alert("123")); 

If you call it by this method:

 function testone(a) {a();} 

then, naturally, the arguments of the passed function do not work, what to do?

The worst thing is that I remember exactly that I once encountered such a problem a long time ago and found a solution. There was some standard function, for example, standart (a), which performed the transferred function with arguments, but now I can’t remember as it is called.

  • Maybe this: function testone (a) {a.apply (this, [arg1, arg2]); } Or like this: function testone (a) {a.call (this, arg1, arg2); } - ReinRaus
  • No, there was the whole point that I didn’t have to calculate the arguments, but I’m passing the function of the testone to the function that is already ready for the call with the already specified arguments, there must be a way, probably, to call it without listing the arguments. Call it in such a way, as shown by you, I can do this: a (arg1, arg2, arg3, ...) - GeneralProger

2 answers 2

In your example ( testone(alert("123")); ), you pass not the function itself, but the result of its execution. The function call occurs at the moment of its transfer in this form as an argument. If you did not need to call a function with certain arguments, the transfer and subsequent call would look like this:

 function testone(f) { f(); } testone(alert); 

But if you need to pass arguments to the called function, and you do not want to pass them separately from the function, then you need to hide them in the closure:

 function testone(f) { f(); } testone(function() { alert("123"); }); 

Now you pass to testone function, inside of which is hidden the call of the function you need with the arguments you need.

Some literature:

    Perhaps the bind() method will help you - http://learn.javascript.ru/bind