在javascript中按值传递匿名函数?
我有一个函数接受匿名函数作为参数并将其设置为变量(作用域)以供参考。 然后,我尝试使用该引用执行另一个函数,但它显然失败了,因为该函数超出了范围。
我想知道是否有人知道一种简单的方法可以将匿名函数作为匿名函数直接传递,从而避免范围问题?
编辑:澄清一下,el 元素是在函数与参数列表隔离后定义的。 此外,el 元素也是参数列表的一部分。 如果这段代码只有我一个人使用,我可能会使用两个参数列表,第二个参数是数组或哈希对象,但不幸的是,这段代码将被一些不太熟悉 JS 甚至编码的人使用对于这个问题。
谢谢您的帮助!
这是我的代码的相关部分:
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);
}
这是缩写的,所以只需相信 el 已定义。 JAS.Globals.eventListen只是一个智能addEventListener。
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. Additionally, the el element is a part of the arguments list as well. 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.
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.
JAS.Globals.eventListen is just an intelligent addEventListener.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
您可以这样做:
for (var i=0;i<arguments.length;i++){
if (typeof(arguments[i])=='function'){
JAS.Globals.eventListen('click',el,arguments[i]);
break;
}
}
或者如果您由于某种原因需要在循环后分配事件:
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);
我不是 JS 专家,不确定这是否对您的情况有帮助。
但是,由于您要求一种“按值”传递函数的方法...您可以重新组合该函数,使其再次变为匿名,如下所示:
JAS.Globals.eventListen('click', el,
new Function(tf.toString() + tf.name + "();")
最后一部分的作用是从原始函数创建一个新函数从 tf.toString() 获取的源代码将打印出具有相同函数名称的函数定义代码,但在即将创建的函数的范围内。
然后您立即使用 tf.name + "();" 调用该函数。
请注意,如果该函数没有名称,那么您可以将其包装为内联函数调用,即:
new Function("(" + tf.toString() + ")();");
包装它:
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
在 Firebug 中尝试一下以了解我的意思。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
这将一个数组返回到 tf. eventListen 是否需要一个数组? 如果不使用:-
既然你的其他参数似乎没有任何其他用途,为什么你还要使用 splice ?
This returns an array into tf. Is eventListen expecting an array? If not use:-
Since you don't seem to have any other uses for your other arguments why are you using splice anyway?