Pass anonymous function by value in javascript?

I have a function that takes an anonymous function as an argument and sets it to a variable (scope) for reference. Then I try to execute another function with this link, but it clearly fails as this function is out of scope.

I was wondering if anyone knew of a simple way to pass an anonymous function as an anonymous function while avoiding the scoping problem?

EDIT . To clarify, the el element is defined after the function is separated from the argument list. In addition, el is also part of the argument list. If this code were only used by me, I would probably use a list of two arguments and the second argument was an array or hash object, but unfortunately this code will be used by some people who are less familiar with JS or even coding in this relation.

Thanks for the help!

Here is the relevant part of my code:

locate : function(){
  if ((!arguments)||(!arguments.length)) return;
  arguments = [].splice.call(arguments,0); //This just converts arguments into an array
  for (var i=0;i<arguments.length;i++){
    if (typeof(arguments[i])=='function'){
      var tf = arguments.splice(i,1);
      break;
    };
  };
  if (!tf) return;
  JAS.Globals.eventListen('click',el,tf);
}

      

This is shorthand, so just trust el is defined. JAS.Globals.eventListen is just an intelligent addEventListener.

+1


source to share


3 answers


 var tf = arguments.splice(i,1)

      

Returns an array in tf. Is eventListen expecting an array? If not use: -



 var tf = arguments.splice(i,1)[0]

      

Since you have no other uses for your other arguments, why are you using splice anyway?

+3


source


You can do:

for (var i=0;i<arguments.length;i++){
    if (typeof(arguments[i])=='function'){
        JAS.Globals.eventListen('click',el,arguments[i]);
        break;
    }
}

      



or if you need to schedule an event after the loop for any reason:

var tf;
for (var i=0;i<arguments.length;i++){
    if (typeof(arguments[i])=='function'){
        tf = arguments[i];
        break;
    }
}
if (!tf) return;
JAS.Globals.eventListen('click',el,tf);

      

0


source


I'm not a JS expert, not sure if this will help in your case.

But since you are asking for a way to pass the function "by value" ... you can re-create the function to make it anonymous again, for example:

JAS.Globals.eventListen('click', el,
    new Function(tf.toString() + tf.name + "();")

      

What this last part does is that it creates a new function from the original source, derived from tf.toString()

, which will output the function definition code with the same function name, but into the scope of the created function.

And then you immediately call this very function using tf.name + "();"

.

Note that if the function has no name, you can wrap it in an inline function call, that is:

new Function("(" + tf.toString() + ")();");

      

Wrap:

new Function(tf.name
    ? (tf.toString() + tf.name + "();")  // func has a name, call by name
    : ("(" + tf.toString() + ")();")     // func don't have a name, call inline

      

Try Firebug to see what I mean.

0


source







All Articles