Is it possible to pass a javascript function with parameters as a parameter?
是否可以将带有参数的javascript函数作为参数传递?
Example:
例子:
$(edit_link).click( changeViewMode( myvar ) );
7 个解决方案
#1
185
Use a "closure":
使用一个“关闭”:
$(edit_link).click(function(){ return changeViewMode(myvar); });
This creates an anonymous temporary function wrapper that knows about the parameter and passes it to the actual callback implementation.
这将创建一个匿名临时函数包装器,该包装器了解参数并将其传递给实际的回调实现。
#2
31
Use Function.prototype.bind()
. Quoting MDN:
使用bind()。引用中数:
The
bind()
method creates a new function that, when called, has itsthis
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.bind()方法创建了一个新函数,当调用该函数时,该函数将其关键字设置为提供的值,并在调用新函数时提供任何参数之前设置给定的参数序列。
It is supported by all major browsers, including IE9+.
所有主要浏览器都支持它,包括IE9+。
Your code should look like this:
您的代码应该如下所示:
$(edit_link).click(changeViewMode.bind(null, myvar));
Side note: I assume you are in global context, i.e. this
variable is window
; otherwise use this
instead of null
.
附注:我假设你在全局上下文中,即这个变量是窗口;否则用这个代替null。
#3
15
No, but you can pass one without parameters, and do this:
没有,但是你可以不带参数地传递一个,这样做:
$(edit_link).click(
function() { changeViewMode(myvar); }
);
So you're passing an anonymous function with no parameters, that function then calls your parameterized function with the variable in the closure
所以你传递了一个没有参数的匿名函数,这个函数调用了你的参数化函数,变量在闭包中
#4
8
Yes, like this:
是的,是这样的:
$(edit_link).click(function() { changeViewMode(myvar) });
#5
2
You can do this
你可以这样做
var message = 'Hello World';
var callback = function(){
alert(this)
}.bind(message);
and then
然后
function activate(callback){
callback && callback();
}
activate(callback);
Or if your callback contains more flexible logic you can pass object.
或者如果回调包含更灵活的逻辑,则可以传递对象。
演示
#6
1
This is an example following Ferdinand Beyer's approach:
这是费迪南德·拜尔的一个例子:
function function1()
{
function2(function () { function3("parameter value"); });
}
function function2(functionToBindOnClick)
{
$(".myButton").click(functionToBindOnClick);
}
function function3(message) { alert(message); }
In this example the "parameter value" is passed from function1 to function3 through function2 using a function wrap.
在本例中,“参数值”通过函数包装从function1传递到function3。
#7
0
Or if you are using es6 you should be able to use an arrow function
或者如果你使用es6,你应该能够使用一个箭头函数。
$(edit_link).click(() => changeViewMode(myvar));
#1
185
Use a "closure":
使用一个“关闭”:
$(edit_link).click(function(){ return changeViewMode(myvar); });
This creates an anonymous temporary function wrapper that knows about the parameter and passes it to the actual callback implementation.
这将创建一个匿名临时函数包装器,该包装器了解参数并将其传递给实际的回调实现。
#2
31
Use Function.prototype.bind()
. Quoting MDN:
使用bind()。引用中数:
The
bind()
method creates a new function that, when called, has itsthis
keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.bind()方法创建了一个新函数,当调用该函数时,该函数将其关键字设置为提供的值,并在调用新函数时提供任何参数之前设置给定的参数序列。
It is supported by all major browsers, including IE9+.
所有主要浏览器都支持它,包括IE9+。
Your code should look like this:
您的代码应该如下所示:
$(edit_link).click(changeViewMode.bind(null, myvar));
Side note: I assume you are in global context, i.e. this
variable is window
; otherwise use this
instead of null
.
附注:我假设你在全局上下文中,即这个变量是窗口;否则用这个代替null。
#3
15
No, but you can pass one without parameters, and do this:
没有,但是你可以不带参数地传递一个,这样做:
$(edit_link).click(
function() { changeViewMode(myvar); }
);
So you're passing an anonymous function with no parameters, that function then calls your parameterized function with the variable in the closure
所以你传递了一个没有参数的匿名函数,这个函数调用了你的参数化函数,变量在闭包中
#4
8
Yes, like this:
是的,是这样的:
$(edit_link).click(function() { changeViewMode(myvar) });
#5
2
You can do this
你可以这样做
var message = 'Hello World';
var callback = function(){
alert(this)
}.bind(message);
and then
然后
function activate(callback){
callback && callback();
}
activate(callback);
Or if your callback contains more flexible logic you can pass object.
或者如果回调包含更灵活的逻辑,则可以传递对象。
演示
#6
1
This is an example following Ferdinand Beyer's approach:
这是费迪南德·拜尔的一个例子:
function function1()
{
function2(function () { function3("parameter value"); });
}
function function2(functionToBindOnClick)
{
$(".myButton").click(functionToBindOnClick);
}
function function3(message) { alert(message); }
In this example the "parameter value" is passed from function1 to function3 through function2 using a function wrap.
在本例中,“参数值”通过函数包装从function1传递到function3。
#7
0
Or if you are using es6 you should be able to use an arrow function
或者如果你使用es6,你应该能够使用一个箭头函数。
$(edit_link).click(() => changeViewMode(myvar));