简体   繁体   English

通过javascript中的值传递匿名函数?

[英]Pass anonymous function by value in javascript?

I have a function that accepts an anonymous function as an argument and sets it to a variable (scoped) for reference. 我有一个接受匿名函数作为参数的函数,并将其设置为变量(作用域)以供参考。 I then try to execute another function with that reference but it obviously fails since that function is out of scope. 然后,我尝试使用该引用执行另一个功能,但由于该功能超出范围,因此显然失败。

I was wondering if anyone knew of a simple way of passing the anonymous function straight through as an anonymous function, avoiding the scoping issue? 我想知道是否有人知道将匿名函数作为匿名函数直接传递的简单方法,从而避免了范围问题?

EDIT : To clarify, the el element is defined after the function is isolated from the arguments list. 编辑 :为澄清起见,在函数与参数列表隔离后定义了el元素。 Additionally, the el element is a part of the arguments list as well. 另外,el元素也是参数列表的一部分。 Were this code to be used by only me, I would likely have used a two argument list with the second argument being an array or hash object but unfortunately this code is going to be used by some folks who are less familiar with JS or even coding for that matter. 如果仅由我使用此代码,我可能会使用两个参数列表,而第二个参数是数组或哈希对象,但是不幸的是,该代码将由不熟悉JS甚至编码的一些人使用对于这个问题。

Thanks for the help! 谢谢您的帮助!

Here is the relevant portion 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 abbreviated so just trust that el is defined. 这是缩写,因此只相信定义了el。 JAS.Globals.eventListen is just an intelligent addEventListener. JAS.Globals.eventListen只是一个智能的addEventListener。

 var tf = arguments.splice(i,1)

This returns an array into tf. 这会将数组返回到tf。 Is eventListen expecting an array? eventListen是否需要数组? If not use:- 如果不使用:-

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

Since you don't seem to have any other uses for your other arguments why are you using splice anyway? 既然您的其他参数似乎没有其他用途,那么为什么仍要使用拼接呢?

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 assign the event after the loop for some 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);

I am not a JS expert, not sure if this will help in your case. 我不是JS专家,所以不确定这是否对您有帮助。

But since you are asking for a way to pass a function "by value"... you can re-compose the function so it becomes anonymous again, like this: 但是,由于您正在寻求一种“按值”传递函数的方法……您可以重新编写该函数,使其再次变为匿名,如下所示:

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

What that last part do is that it creates a new function from the original source obtained from tf.toString() which will prints out the function's definition code with the same function name, but in the scope of the about-to-be-created function. 最后一部分要做的是从从tf.toString()获得的原始源创建一个新函数,该函数将打印出具有相同函数名但在即将创建的范围内的函数的定义代码功能。

And then you immediately calls that very function with tf.name + "();" 然后立即使用tf.name + "();"调用该函数tf.name + "();" .

Note that if the function doesn't have a name, then you can wraps it in as an inline function call, ie: 请注意,如果函数没有名称,则可以将其包装为内联函数调用,即:

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

Wrapping it up: 包装起来:

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 it out in Firebug to see what I mean. 在Firebug中尝试一下,看看我的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM