以下内容主要摘自[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/

[2]http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/?WPACFallback=1&WPACRandom=1413199645166

理解callback function in javascript的更多相关文章

  1. JavaScript callback function 回调函数的理解

    来源于:http://mao.li/javascript/javascript-callback-function/ 看到segmentfault上的这个问题 JavaScript 回调函数怎么理解, ...

  2. JavaScript callback function 理解

    看到segmentfault上的这个问题 JavaScript 回调函数怎么理解,觉得大家把异步和回调的概念混淆在一起了.做了回答: 我觉得大家有点把回调(callback)和异步(asynchron ...

  3. C语言中的回调函数(Callback Function)

    1 定义和使用场合 回调函数是指 使用者自己定义一个函数,实现这个函数的程序内容,然后把这个函数(入口地址)作为参数传入别人(或系统)的函数中,由别人(或系统)的函数在运行时来调用的函数.函数是你实现 ...

  4. (C/C++) Callback Function 回调(diao)函数

    原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial ...

  5. 【JavaScript】Understanding callback functions in Javascript

    Callback functions are extremely important in Javascript. They’re pretty much everywhere. Originally ...

  6. UNDERSTANDING CALLBACK FUNCTIONS IN JAVASCRIPT

    转自: http://recurial.com/programming/understanding-callback-functions-in-javascript/ Callback functio ...

  7. Dreamweaver 扩展开发: Calling a C++ function from JavaScript

    After you understand how C-level extensibility works in Dreamweaver and its dependency on certain da ...

  8. callback function(回调函数) - 术语

    回调函数(CallBack Function)的定义: [todo] 下面是callback的一个场景,linux内核.LwIP里也有使用callback funcs.

  9. JS之Callback function(回调函数)

    JS中的回调函数: 1.概念: 函数a有一个参数,这个参数是个函数b,当函数a执行完以后执行函数b,那么这个过程就叫回调,即把函数作为参数传入到另一个函数中,这个函数就是所谓的回调函数. 2.举例: ...

随机推荐

  1. [转]C#反射-Assembly.Load、LoadFrom与LoadFile进阶

    关于.NET中的反射,常用的有三个方法: Assembly.Load()Assembly.LoadFrom()Assembly.LoadFile() 下面说说这三个方法的区别和一些细节问题 1. As ...

  2. 自定义Docker容器的 hostname

    自定义Docker容器的 hostname   作者: John Deng 原创内容,欢迎传播,请注明出处:http://www.cnblogs.com/johnd/p/set-docker-host ...

  3. Android开发环境的搭建

    在学习android时,环境的搭建是学习android的第一步,为了记住第一步特写了这篇文章. 第一步,安装jdk,因为eclipse的运行需要jdk才可以,所以jdk的安装时第一步.安装过程和普通的 ...

  4. yaf设置命名空间

    修改yaf配置文件 文件是:yaf.ini extension=yaf.so yaf.use_namespace=1 index文件. 目录是application/controllers/Index ...

  5. 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

    错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...

  6. 以空白符结尾的 alias

    网上经常有人问这个问题:为什么我写的 alias 在 sudo 下就不管用了? $ alias 'll=ls -l' $ sudo ll a-private-dir sudo: ll: command ...

  7. 大熊君学习html5系列之------Online && Offline(在线状态检测)

    一,开篇分析 Hi,大家好,给大家拜个晚年!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例,让大家一步一步的 ...

  8. 试图加载格式不正确的程序。 (异常来自HRESULT:0x8007000B)

    异常来自HRESULT:0x8007000B   缘由:在64位操作系统下IIS发布32位的项目,报“项目依赖的dll无法读取,试图加载格式不正确的程序”错误. 原因:程序集之间的通讯要么全是64位环 ...

  9. nyoj 776 删除元素

    删除元素 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 题意很简单,给一个长度为n的序列,问至少删除序列中多少个数,使得删除后的序列中的最大值<= 2*最小值 输 ...

  10. linux 最近使用的命令

    vi 文件名   >>  按 i 键,进行编辑,编辑完,按ESC键,再按:q代表不保存退出,按:wq代表保存退出. ps aux|grep java >>  搜索目前启动的ja ...