我试图传递一些参数的函数用作回调,我怎么能这样做?

这是我的尝试: 函数tryMe(param1, param2) { Alert (param1 + " and " + param2); } 函数callbackTester(回调,param1, param2) { 回调(param1 param2); } callbackTester(tryMe, "hello", "goodbye");


当前回答

这也可以:

回调函数 函数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); });

其他回答

当你有一个回调函数,它将被你的代码以外的东西调用,具有特定数量的参数,你想传递额外的参数,你可以传递一个包装器函数作为回调,并在包装器内部传递额外的参数。

function login(accessedViaPopup) {
    //pass FB.login a call back function wrapper that will accept the
    //response param and then call my "real" callback with the additional param
    FB.login(function(response){
        fb_login_callback(response,accessedViaPopup);
    });
}

//handles respone from fb login call
function fb_login_callback(response, accessedViaPopup) {
    //do stuff
}

如果你想要一些更一般的东西,你可以像这样使用arguments变量:

函数tryMe(param1, param2) { Alert (param1 + " and " + param2); } 函数callbackTester(回调){ 回调参数(参数[1],[2]); } callbackTester(tryMe, "hello", "goodbye");

但除此之外,你的例子工作得很好(参数[0]可以用来代替测试器中的回调)

在这个简单的例子中使用curry函数。

const BTN = document.querySelector('button') const RES = document.querySelector('p') const changeText = newText => () => { RES.textContent = newText } BTN。addEventListener(“点击”,changeText(点击!)) <按钮> ClickMe > < /按钮 < p >不点击< p >

带有任意数量参数和回调上下文的问题的代码:

function SomeFunction(name) {
    this.name = name;
}
function tryMe(param1, param2) {
    console.log(this.name + ":  " + param1 + " and " + param2);
}
function tryMeMore(param1, param2, param3) {
    console.log(this.name + ": " + param1 + " and " + param2 + " and even " + param3);
}
function callbackTester(callback, callbackContext) {
    callback.apply(callbackContext, Array.prototype.splice.call(arguments, 2));
}
callbackTester(tryMe, new SomeFunction("context1"), "hello", "goodbye");
callbackTester(tryMeMore, new SomeFunction("context2"), "hello", "goodbye", "hasta la vista");

// context1: hello and goodbye
// context2: hello and goodbye and even hasta la vista

回调函数将由其他函数调用,而不是由您自己的代码调用,并且您希望添加其他参数的场景的新版本。

例如,假设您有许多带有成功回调和错误回调的嵌套调用。我将在这个例子中使用角承诺,但任何带回调的javascript代码都是相同的目的。

someObject.doSomething(param1, function(result1) {
  console.log("Got result from doSomething: " + result1);
  result.doSomethingElse(param2, function(result2) {
    console.log("Got result from doSomethingElse: " + result2);
  }, function(error2) {
    console.log("Got error from doSomethingElse: " + error2);
  });
}, function(error1) {
  console.log("Got error from doSomething: " + error1);
});

现在,您可能希望通过定义一个函数来记录错误,保留错误的起源以用于调试,从而整理代码。这是你如何继续重构你的代码:

someObject.doSomething(param1, function (result1) {
  console.log("Got result from doSomething: " + result1);
  result.doSomethingElse(param2, function (result2) {
    console.log("Got result from doSomethingElse: " + result2);
  }, handleError.bind(null, "doSomethingElse"));
}, handleError.bind(null, "doSomething"));

/*
 * Log errors, capturing the error of a callback and prepending an id
 */
var handleError = function (id, error) {
  var id = id || "";
  console.log("Got error from " + id + ": " + error);
};

调用函数仍然会在回调函数参数之后添加error参数。