Callback functions are extremely important in Javascript. They’re pretty much everywhere. Originally coming from a more traditional C/Java background I had trouble with this (and the whole idea of asynchronous programming), but I’m starting to get the hang of it. Strangely, I haven’t found any good introductions to callback functions online — I mainly found bits of documentation on the call() and apply() functions, or brief code snippits demonstrating their use — so, after learning the hard way I decided to try to write a simple introduction to callbacks myself.
Functions are objects
To understand callback functions you first have to understand regular functions. This might seen like a “duh” thing to say, but functions in Javascript are a bit odd.
Functions in Javascript are actually objects. Specifically, they’re Function
objects created with the Function
constructor. A Function
object contains a string which contains the Javascript code of the function. If you’re coming from a language like C or Java that might seem strange (how can code be a string?!) but it’s actually run-of-the-mill for Javascript. The distinction between code and data is sometimes blurred.
1 |
// you can create a function by passing the |
2 |
// Function constructor a string of code |
3 |
var func_multiply = new Function( "arg1" , "arg2" , "return arg1 * arg2;" ); |
4 |
func_multiply(5,10); // => 50 |
One benefit of this function-as-object concept is that you can pass code to another function in the same way you would pass a regular variable or object (because the code is literally just an object).
Passing a function as a callback
Passing a function as an argument is easy.
01 |
// define our function with the callback argument |
02 |
function some_function(arg1, arg2, callback) { |
03 |
// this generates a random number between |
05 |
var my_number = Math.ceil(Math.random() * |
06 |
(arg1 - arg2) + arg2); |
07 |
// then we're done, so we'll call the callback and |
12 |
some_function(5, 15, function (num) { |
13 |
// this anonymous function will run when the |
15 |
console.log( "callback called! " + num); |
It might seem silly to go through all that trouble when the value could just be returned normally, but there are situations where that’s impractical and callbacks are necessary.
Don’t block the way
Traditionally functions work by taking input in the form of arguments and returning a value using a return statement (ideally a single return statement at the end of the function: one entry point and one exit point). This makes sense. Functions are essentially mappings between input and output.
Javascript gives us an option to do things a bit differently. Rather than wait around for a function to finish by returning a value, we can use callbacks to do it asynchronously. This is useful for things that take a while to finish, like making an AJAX request, because we aren’t holding up the browser. We can keep on doing other things while waiting for the callback to be called. In fact, very often we are required (or, rather, strongly encouraged) to do things asynchronously in Javascript.
Here’s a more comprehensive example that uses AJAX to load an XML file, and uses the call() function to call a callback function in the context of the requested object (meaning that when we call the this
keyword inside the callback function it will refer to the requested object):
01 |
function some_function2(url, callback) { |
02 |
var httpRequest; // create our XMLHttpRequest object |
03 |
if (window.XMLHttpRequest) { |
04 |
httpRequest = new XMLHttpRequest(); |
05 |
} else if (window.ActiveXObject) { |
06 |
// Internet Explorer is stupid |
08 |
ActiveXObject( "Microsoft.XMLHTTP" ); |
11 |
httpRequest.onreadystatechange = function () { |
12 |
// inline function to check the status |
14 |
// this is called on every state change |
15 |
if (httpRequest.readyState === 4 && |
16 |
httpRequest.status === 200) { |
17 |
callback.call(httpRequest.responseXML); |
18 |
// call the callback function |
21 |
httpRequest.open( 'GET' , url); |
25 |
some_function2( "text.xml" , function () { |
28 |
console.log( "this will run before the above callback" ); |
In this example we create the httpRequest
object and load an XML file. The typical paradigm of returning a value at the bottom of the function no longer works here. Our request is handled asynchronously, meaning that we start the request and tell it to call our function when it finishes.
We’re using two anonymous functions here. It’s important to remember that we could just as easily be using named functions, but for sake of brevity they’re just written inline. The first anonymous function is run every time there’s a state change in our httpRequest
object. We ignore it until the state is 4 (meaning it’s done) and the status is 200 (meaning it was successful). In the real world you’d want to check if the request failed, but we’re assuming the file exists and can be loaded by the browser. This anonymous function is assigned tohttpRequest.onreadystatechange
, so it is not run right away but rather called every time there’s a state change in our request.
When we finally finish our AJAX request, we not only run the callback function but we use the call()
function. This is a different way of calling a callback function. The method we used before of just running the function would work fine here, but I thought it would be worth demonstrating the use of the call()
function. Alternatively you could use the apply()
function (the difference between the two is beyond the scope of this tutorial, but it involves how you pass arguments to the function).
The neat thing about using call()
is that we set the context in which the function is executed. This means that when we use the this
keyword inside our callback function it refers to whatever we passed as the first argument for call()
. In this case, when we refer to this inside our anonymous callback function we are referring to the responseXML
from the AJAX request.
Finally, the second console.log statement will run before the first, because the callback isn’t executed until the request is over, and until that happens the rest of the code goes right on ahead and keeps running.
Wrapping it up
Hopefully now you should understand callbacks well enough to use them in your own code. I still find it hard to structure code that is based around callbacks (it ends up looking like spaghetti… my mind is too accustomed to regular structured programming), but they’re a very powerful tool and one of the most interesting parts of the Javascript language.
http://recurial.com/programming/understanding-callback-functions-in-javascript/
- UNDERSTANDING CALLBACK FUNCTIONS IN JAVASCRIPT
转自: http://recurial.com/programming/understanding-callback-functions-in-javascript/ Callback functio ...
- 【总结】IE和Firefox的Javascript兼容性总结(转)
文章转自:http://www.cnblogs.com/wiky/archive/2010/01/09/IE-and-Firefox-Javascript-compatibility.html 长久以 ...
- 【转】ajax 跨域 headers JavaScript ajax 跨域请求 +设置headers 实践
解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头.正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现. 此处手札 供后人参 ...
- 【总结】IE和Firefox的Javascript兼容性总结
长久以来JavaScript兼容性一直是Web开发者的一个主要问题.在正式规范.事实标准以及各种实现之间的存在的差异让许多开发者日夜煎熬.为此,主要从以下几方面差异总结IE和Firefox的Javas ...
- 【JS】369- 20个常用的JavaScript字符串方法
点击上方"前端自习课"关注,学习起来~ 作者:前端小智 https://segmentfault.com/a/1190000020204425 本文主要介绍一些最常用的JS字符串函 ...
- 【repost】学JS必看-JavaScript数据结构深度剖析
JavaScript以其强大灵活的特点,被广泛运用于各种类型的网站上.一直以来都没怎么好好学JS,只是略懂皮毛,看这篇文章时有读<PHP圣经>的感觉,作者深入浅出.生动形象地用各种实例给我 ...
- 【转】45个实用的JavaScript技巧、窍门和最佳实践
原文:https://colobu.com/2014/09/23/45-Useful-JavaScript-Tips,-Tricks-and-Best-Practices/ 目录 [−] 列表 第一次 ...
- 【转】最流行的编程语言JavaScript能做什么?
本文转自互联网! 首先很遗憾的一点是,“PHP虽然是最好的语言”,但是它不是最流行的语言. 对不起的还有刚刚在4月TIOBE编程语言排行榜上榜的各个语言: 你们都很棒,但是你们都担当不了这个大任. 开 ...
- 【原创】web端高德地图javascript API的调用
关于第三放地图的使用,腾讯.百度.高德 具体怎么选择看你自己怎么选择了. 高德地图开放平台:http://lbs.amap.com/ 本次使用的是高德的javascript API http://lb ...
随机推荐
- “iOS 推送通知”详解:从创建到设置到运行
这是一篇编译的文章,内容均出自Parse.com的iOS开发教程,同时作者还提供了视频讲解.本文将带领开发者一步一步向着iOS推送通知的深处探寻,掌握如何配置iOS推送通知的奥义. 介绍一点点背景资料 ...
- 设计模式 单件-Singleton
单件模式 Singleton 什么时候使用?当需要独一无二的对象时,请想起他. 举例:线程池(threadpool),缓存(cache),对话框,处理偏好设置和注册表(registry)的对象,驱动程 ...
- JavaScript高级程序设计(第三版)第五章 引用类型
5.2 Array类型 var colors = new Array(3); //创建一个包含3项的数组 var names = new Array("Greg"); //创建一个 ...
- Best Practices for Speeding Up Your Web Site
The Exceptional Performance team has identified a number of best practices for making web pages fast ...
- CodeIgniter 3.0+ 部署linux环境 session报错
codeigniter Message: mkdir(): Invalid path Filename: drivers/Session_files_driver.php 看起来像权限问题,在默认情况 ...
- 企业网管软件实战之SolarWinds LANsurveyor
SolarWinds LANsurveyor是一款比较容易掌握的网络管理软件,他能自动探索你的LAN或WAN,并生成全面的,易于浏览的集成了OSI 2层和 3层 拓扑数据的网络图表.其主要功能有: 1 ...
- 第三百五十八天 how can I 坚持
万事要有度,不要话唠,也不能不说,把握好分寸,今天貌似又说多了. 加了天班,理了个发,还有老爸明天来北京. 还有同学聚会没去,还有金龙让去吃鱼,没去. 还有.小米视频通话还行,能远程控制桌面, 还有, ...
- Uvalive 4865 Data Recovery 最大流
题意就是 给一个50 * 50的矩阵 然后给出每行每列元素的和 和一个初始矩阵 矩阵中有些是未知,有些是已知 然后我们求目标矩阵就是把能确定的元素的值求出来,实在不能确定的就置为-1 所有矩阵元素的值 ...
- thinkphp过滤html、script
使用tp3.1版本 1.APP/common 自定义函数 function filter_default(&$value){ $value = htmlspecialchars($value) ...
- iOS设置导航栏样式(UINavigationController)
//设置导航栏baritem和返回baiitem样式 UIBarButtonItem *barItem = [UIBarButtonItem appearance]; //去掉返回按钮上的字 [bar ...