我试图传递一些参数的函数用作回调,我怎么能这样做?
这是我的尝试: 函数tryMe(param1, param2) { Alert (param1 + " and " + param2); } 函数callbackTester(回调,param1, param2) { 回调(param1 param2); } callbackTester(tryMe, "hello", "goodbye");
我试图传递一些参数的函数用作回调,我怎么能这样做?
这是我的尝试: 函数tryMe(param1, param2) { Alert (param1 + " and " + param2); } 函数callbackTester(回调,param1, param2) { 回调(param1 param2); } callbackTester(tryMe, "hello", "goodbye");
当前回答
只需使用bind()函数,该函数主要用于设置this值。但是,我们也可以使用它来传递参数而不调用函数,因为bind()返回一个带有提供的参数序列的新函数。
例子:
函数foo(param1, param2, param3) { console.log(param1, param2, param3); } setTimeout (foo。bind(null, 'foo', 'bar', 'ba '), 1000);
在上面的代码片段中,setTimeout函数有两个参数,一个是回调函数,另一个是函数被调用的最小时间(以ms为单位),因此在传递回调函数时,我们将使用bind并指定参数
注意:bind的第一个参数是我们想要为此设置的值,因为我们对它不感兴趣,传递的是null,所以bind中的后续参数将是回调的参数。
其他回答
如果你想要一些更一般的东西,你可以像这样使用arguments变量:
函数tryMe(param1, param2) { Alert (param1 + " and " + param2); } 函数callbackTester(回调){ 回调参数(参数[1],[2]); } callbackTester(tryMe, "hello", "goodbye");
但除此之外,你的例子工作得很好(参数[0]可以用来代替测试器中的回调)
你的问题不清楚。如果你在问如何以更简单的方式做到这一点,你应该看看ECMAScript第5版方法.bind(),它是Function.prototype的成员。使用它,你可以做这样的事情:
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
function callbackTester (callback) {
callback();
}
callbackTester(tryMe.bind(null, "hello", "goodbye"));
你也可以使用下面的代码,它会在当前浏览器中不可用时添加方法:
// From Prototype.js
if (!Function.prototype.bind) { // check if native implementation available
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments),
object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
}
例子
bind() - PrototypeJS文档
//Suppose function not taking any parameter means just add the GetAlterConfirmation(function(result) {});
GetAlterConfirmation('test','messageText',function(result) {
alert(result);
}); //Function into document load or any other click event.
function GetAlterConfirmation(titleText, messageText, _callback){
bootbox.confirm({
title: titleText,
message: messageText,
buttons: {
cancel: {
label: '<i class="fa fa-times"></i> Cancel'
},
confirm: {
label: '<i class="fa fa-check"></i> Confirm'
}
},
callback: function (result) {
return _callback(result);
}
});
这也可以:
回调函数 函数tryMe(param1, param2) { 警报(参数 1 + “ 和 ” + 参数 2); } 回调执行器 函数回调测试器(回调) { 回调(); } 测试功能 callbackTester(function() { tryMe(“hello”,“goodbye”); });
另一个场景:
// callback function function tryMe(param1, param2, param3) { alert(param1 + " and " + param2 + " " + param3); } // callback executer function callbackTester(callback) { //this is the more obivous scenario as we use callback function //only when we have some missing value //get this data from ajax or compute var extraParam = "this data was missing"; //call the callback when we have the data callback(extraParam); } // test function callbackTester(function(k) { tryMe("hello", "goodbye", k); });
如果你不确定要将多少参数传递给回调函数,请使用apply function。
function tryMe (param1, param2) {
alert (param1 + " and " + param2);
}
function callbackTester(callback,params){
callback.apply(this,params);
}
callbackTester(tryMe,['hello','goodbye']);