博客
关于我
回调函数(callback function)
阅读量:369 次
发布时间:2019-03-05

本文共 927 字,大约阅读时间需要 3 分钟。

计算机编程中,”回调“是可执行代码的参考,或者是一段可执行程序,”回调“被当做参数传入另外的代码中。这种”回调“机制允许软件底层代码调用上层定义的函数。

很多人对”回调“感到困惑是因为这个”回调“这个名字在作怪。

一个回调方法可以作为参数传入另外一个方法中,回调方法的执行是某些事件所激发的。参数的”调回“本质是,一旦父方法执行完,作为参数的回调方法(回调函数)就会被调用。也就是说父方法调回执行作为参数传过来的方法。

//An innocuous looking method which will become known as a callback method//because of the way in which we will invoke it.function meaningOfLife() {    log("The meaning of life is: 42");}//An innocuous looking method which just takes an int and prints it to screen, and takes a function reference to be executed when printANumber completesfunction printANumber(int number, function callbackFunction()) {    print("The number you provided is: " + number);}function event() {   printANumber(6, meaningOfLife());}

The number you provided is: 6The meaning of life is: 42


之所以叫回调函数,是因为在那些含有指针概念的编程语言中有这样的叫法。如果你的编程语言没有指针概念,那么就可以不去关心回调函数的执行细节。仅仅把它理解成一个函数作为参数被另外一个函数调用就成了。因此,无论父函数什么时候被调用(不论什么情况:按钮点击,定时器到时),当父函数执行完后,回调函数就会被调用。

转载地址:http://zcuwz.baihongyu.com/

你可能感兴趣的文章
(C++11/14/17学习笔记):线程启动、结束,创建线程多法、join,detach
查看>>
leetcode 14 最长公共前缀
查看>>
做做Java
查看>>
C++并发与多线程(一)
查看>>
java一些基本程序
查看>>
vue-依赖-点击复制
查看>>
LeetCode 116填充每个节点的下一个右侧结点指针
查看>>
2021-4-28【PTA】【L2-1 包装机 (25 分)】
查看>>
Arduino mega2560+MPU6050利用加速度值控制舵机
查看>>
紫书——蛇形填数
查看>>
A Guide to Node.js Logging
查看>>
webwxbatchgetcontact一个神奇的接口
查看>>
Edge浏览器:你的的内核我的芯
查看>>
【考研英语-基础-简单句】简单句的核心变化_谓语情态
查看>>
Jetson AGX Xavier硬件自启动
查看>>
统计字符数
查看>>
JS 数组的 every()、some() 、filter()、findIndex() 、find()、map()方法
查看>>
JS数据类型的判断
查看>>
实现一个简易Vue(三)Compiler
查看>>
仿小米商城(上)
查看>>