理解callback function in javascript
以下内容主要摘自[1,2]
(1)In javascript, functions are first-class objects, which means functions can be used in a first-class manner like objects, since they are in fact objects themselves: They can be “stored in variables, passed as arguments to functions, created within functions, and returned from functions”。
(2)Callback functions are derived from a programming paradigm called functional programming. At a simple and fundamental level, functional programming is the use of functions as arguments. Functional programming was—and still is, though to a much lesser extent today—seen as an esoteric technique of specially trained, master programmers.
(3)When we pass a callback function as an argument to another function, we are only passing the function definition.
(4)If callback function is a asynchronous function[自己定义的callback函数如果调用了异步函数库,则该函数是一个异步函数;否则,同步函数.例如:node中读取文件的两个函数 fs.readfile() vs fs.readfileSync()], then callback function will be executed later than those code behind the function which called the callback function.
(5)every function in JavaScript has two methods: Call and Apply.
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
04 // arg1 and arg2
05 var my_number = Math.ceil(Math.random() *
06 (arg1 - arg2) + arg2);
07 // then we're done, so we'll call the callback and
08 // pass our result
09 callback(my_number);
10 }
11 // call the function
12 some_function(5, 15, function(num) {
13 // this anonymous function will run when the
14 // callback is called
15 console.log("callback called! " + num);
16 });
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.
Reference
[1]http://recurial.com/programming/understanding-callback-functions-in-javascript/
理解callback function in javascript的更多相关文章
- JavaScript callback function 回调函数的理解
来源于:http://mao.li/javascript/javascript-callback-function/ 看到segmentfault上的这个问题 JavaScript 回调函数怎么理解, ...
- JavaScript callback function 理解
看到segmentfault上的这个问题 JavaScript 回调函数怎么理解,觉得大家把异步和回调的概念混淆在一起了.做了回答: 我觉得大家有点把回调(callback)和异步(asynchron ...
- C语言中的回调函数(Callback Function)
1 定义和使用场合 回调函数是指 使用者自己定义一个函数,实现这个函数的程序内容,然后把这个函数(入口地址)作为参数传入别人(或系统)的函数中,由别人(或系统)的函数在运行时来调用的函数.函数是你实现 ...
- (C/C++) Callback Function 回调(diao)函数
原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial ...
- 【JavaScript】Understanding callback functions in Javascript
Callback functions are extremely important in Javascript. They’re pretty much everywhere. Originally ...
- UNDERSTANDING CALLBACK FUNCTIONS IN JAVASCRIPT
转自: http://recurial.com/programming/understanding-callback-functions-in-javascript/ Callback functio ...
- Dreamweaver 扩展开发: Calling a C++ function from JavaScript
After you understand how C-level extensibility works in Dreamweaver and its dependency on certain da ...
- callback function(回调函数) - 术语
回调函数(CallBack Function)的定义: [todo] 下面是callback的一个场景,linux内核.LwIP里也有使用callback funcs.
- JS之Callback function(回调函数)
JS中的回调函数: 1.概念: 函数a有一个参数,这个参数是个函数b,当函数a执行完以后执行函数b,那么这个过程就叫回调,即把函数作为参数传入到另一个函数中,这个函数就是所谓的回调函数. 2.举例: ...
随机推荐
- javascript学习
代码放在E:\JS\js学习 学习中要学会多查手册 javascript基本介绍 js是用于web开发的脚本语言. 后面统称为 js 脚本语言是什么? 脚本语言不能独立使用,它和html/jsp/ph ...
- 关于LuCi
好吧,又长见识了...相见恨晚的赶脚,恩,居然是我喜欢的lua.其主页在这里:http://luci.subsignal.org/ The initial reason for this projec ...
- NodeJs + mongodb模块demo
代码比较通俗易懂,但是我还是在这个过程中浪费了不少时间,也算是看到了nodejs中异步的一个小坑.未来的坑还有很多,慢慢找坑填坑吧. 参考资料如下: 1.断言模块 : https://nodejs.o ...
- <<< 入侵网站类提权注入教程
---------------------------------------入侵类教程-------------------------------------------------------- ...
- ArcGIS Server开发教程系列(3)切片
切片工作,我们可以一级一级的切,也可以,所有的一块切,Recreate All Tiles这项是说,在没有进行任何的切片工作时,可以选用这项:Recreate Empty Tiles这项是说,如果之前 ...
- Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页、搜索、格式化、自定义按钮
系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...
- wordpress 函数、条件判断以及文件的总结
WordPress基本模板文件 一套完整的WordPress模板应至少具有如下文件: style.css : CSS(样式表)文件 index.php : 主页模板 archive.php : Arc ...
- 解决COM组件80070005错误
前段时间在维护公司以前的项目时遇到一个问题,客户需要添加一个word文档合并功能,按理说这功能比较好实现,只要调用Office自带的COM组件就搞定了,但实际上并非如此,在客户端部署上以后运行报错,提 ...
- html5新增的主体结构元素
1. article 主体结构元素 通常是一篇文章.一个页面.一个独立完整的内容模块一般会带个标题,并放在 header 标签中,article 元素可以互相嵌套,使用频率极高,强调独立性,多注意下 ...
- 2014 Multi-University Training Contest 9#6
2014 Multi-University Training Contest 9#6 Fast Matrix CalculationTime Limit: 2000/1000 MS (Java/Oth ...