当您将函数作为参数传递时,它称为回调函数,并且当您通过此回调函数返回值时,该值就是所传递函数的参数。
function myFunction(val, callback){ if(val == 1){ callback(true); }else{ callback(false); }}myFunction(0, //the true or false are passed from callback() //is getting here as bool// the anonymous function below defines the functionality of the callbackfunction (bool){ if(bool){ alert("do stuff for when value is true"); }else { //this condition is satisfied as 0 passed alert("do stuff for when value is false"); }});
基本上,callbacks()用于异步概念。在特定事件上调用它。
myFunction也是回调函数。例如,它发生在单击事件上。
document.body.addEventListener('click', myFunction);
这意味着,首先将动作分配给其他功能,然后再考虑一下。满足条件时将执行该 *** 作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)