如何将参数传递给绑定方法?绑定与匿名方法 [英] How do I pass arguments to bound methods? bind vs. anonymous methods
本文介绍了如何将参数传递给绑定方法?绑定与匿名方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试用下面带有参数的方法替换方法名称,但这不起作用。
I tried replacing the method name with a method w/ arguments below but this did not work.
// just a minimizer method
function m5(a,b)
{
return document.getElementById(a).onkeypress=b;
}
// On page initialization thse methods are bound to text input boxes
m5('signin_pass',bind_enter_key(event,interface_signin)); // this does not work
m5('upload_file',bind_file_upload);
推荐答案
您可以使用匿名函数执行此操作用正确的参数调用你的函数:
You can do it like this with an anonymous function that calls your function with the correct parameters:
// just a minimizer method
function m5(a,b) {
return document.getElementById(a).onkeypress=b;
}
// On page initialization these methods are bound to text input boxes
m5('signin_pass', function(event) {bind_enter_key(event,interface_signin)}); // this does not work
m5('upload_file', bind_file_upload);
这会创建一个匿名函数,该函数作为函数传递给m5,匿名函数调用你的函数适当的参数。
This creates an anonymous function which is passed to m5 as the function and that anonymous function calls your function with the appropriate parameters.
这篇关于如何将参数传递给绑定方法?绑定与匿名方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文