我试图传递一些参数的函数用作回调,我怎么能这样做?
这是我的尝试: 函数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");
当前回答
在这个简单的例子中使用curry函数。
const BTN = document.querySelector('button') const RES = document.querySelector('p') const changeText = newText => () => { RES.textContent = newText } BTN。addEventListener(“点击”,changeText(点击!)) <按钮> ClickMe > < /按钮 < p >不点击< p >
其他回答
当你有一个回调函数,它将被你的代码以外的东西调用,具有特定数量的参数,你想传递额外的参数,你可以传递一个包装器函数作为回调,并在包装器内部传递额外的参数。
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
}
我试图传递一些参数的函数用作回调,我怎么能这样做?
我认为他是在暗示他想调用这个callbackTester函数(tryMe, "hello", "goodbye")。为此,我们可以使用Rest操作符(…)。该操作符接受函数接收到的参数,并将它们转储到一个实数组中,我们将在回调函数中使用该数组进行访问。
现在,其他一些开发人员可能也认为我们可以使用参数"array"。那很好,但我们要小心。Arguments不是一个真正的数组,而是一个具有length属性的类数组对象。
下面是一个使用Rest操作符的工作代码片段:
函数tryMe(参数){ console.log(参数。加入(',')); } 函数callbackTester(回调,…params) { 回调(params); } callbackTester(tryMe, 'hello', 'goodbye', 'hi again'); callbackTester(tryMe, 'hello', 'goodbye'); callbackTester (tryMe '你好');
这也可以:
回调函数 函数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); });
只需使用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中的后续参数将是回调的参数。
让我给你一个非常简单的使用回调的Node.js风格的例子:
/**
* Function expects these arguments:
* 2 numbers and a callback function(err, result)
*/
var myTest = function(arg1, arg2, callback) {
if (typeof arg1 !== "number") {
return callback('Arg 1 is not a number!', null); // Args: 1)Error, 2)No result
}
if (typeof arg2 !== "number") {
return callback('Arg 2 is not a number!', null); // Args: 1)Error, 2)No result
}
if (arg1 === arg2) {
// Do somethign complex here..
callback(null, 'Actions ended, arg1 was equal to arg2'); // Args: 1)No error, 2)Result
} else if (arg1 > arg2) {
// Do somethign complex here..
callback(null, 'Actions ended, arg1 was > from arg2'); // Args: 1)No error, 2)Result
} else {
// Do somethign else complex here..
callback(null, 'Actions ended, arg1 was < from arg2'); // Args: 1)No error, 2)Result
}
};
/**
* Call it this way:
* Third argument is an anonymous function with 2 args for error and result
*/
myTest(3, 6, function(err, result) {
var resultElement = document.getElementById("my_result");
if (err) {
resultElement.innerHTML = 'Error! ' + err;
resultElement.style.color = "red";
//throw err; // if you want
} else {
resultElement.innerHTML = 'Result: ' + result;
resultElement.style.color = "green";
}
});
以及将呈现结果的HTML:
<div id="my_result">
Result will come here!
</div>
你可以在这里使用它:https://jsfiddle.net/q8gnvcts/ -例如,尝试传递字符串而不是数字:myTest('some string', 6, function(err, result)..看看结果如何。
我希望这个例子对你有所帮助,因为它代表了回调函数的基本概念。