A callback is a function that is passed as an argument to another function and is
executed after its parent function has completed.
这是js里面的解释。意思是“回调是一个函数作为参数传递给另一个函数,其主函数完成后执行”。下面举一个简单的实例代码如下:
1. 基本函数调用
function a(callback){
callback();
}
function b(){
console.info("B");
}
function test(){
a(b);
}
在这里,函数b是以参数形式传给函数a的,那么函数b就叫回调函数。当函数a执行完以后回头去调用函数b
2.使用javascript的call方法实现匿名函数调用
function dosomething(damsg, callback){
console.info(damsg);
if(typeof callback == "function")
callback.call(damsg);
}
funtion test(){
dosomething("回调函数", function(msg){
console.info(msg);
});
}
在这里匿名函数 funtion(msg)就是作为回调函数的参数,首先执行dosomething函数,执行完成以后,回头来执行function(msg)这个函数,也就是所谓的回调函数。