js函数回调

      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)这个函数,也就是所谓的回调函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值