博客
关于我
回调函数(callback function)
阅读量:376 次
发布时间: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/

你可能感兴趣的文章
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>