美文网首页
回调函数

回调函数

作者: meng_281e | 来源:发表于2018-11-01 17:33 被阅读0次
    Q请解释什么叫做回调函数并提供一个简单的例子

    回调函数是一个函数,它被作为参数传入另一个函数,当某些操作结束后,该函数被调用。下面是一个简单的例子,当数组被修改后,调用回调函数打印一行日志。

    function modifyArray(arr, callback) {
      // do something to arr here
      arr.push(100);
      // then execute the callback function that was passed
      callback();
    }
    
    var arr = [1, 2, 3, 4, 5];
    modifyArray(arr, function() {
      console.log("array has been modified", arr);
    });
    

    相关文章

      网友评论

          本文标题:回调函数

          本文链接:https://www.haomeiwen.com/subject/ufgxxqtx.html