以下内容主要摘自[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. Hibernate的session缓存和对象的四种状态

    一.session缓存 说session缓存就得说到JAVA对象的生命周期,当没有任何引用指向一个对象时,对象则可以被gc回收,也就是生命周期结束了 而hibernate获取一个对象后,会将对象存入s ...

  2. 深入理解springAOP

    概念:AOP(Aspect-Oriented Programming)即面向切面编程.它是对传统的OOP(面向对象)编程的一种补充,在OOP中往往一个对象有什么行为我们就定义什么方法,对象与对象之间存 ...

  3. A Quick Introduction to Linux Policy Routing

    A Quick Introduction to Linux Policy Routing 29 May 2013 In this post, I’m going to introduce you to ...

  4. 11月15日下午 ajax返回数据类型为XML数据的处理

    ajax返回数据类型为XML数据的处理 /*XML:可扩展标记语言 HTML:超文本标记语言 标签:<标签名></标签名> 特点: 1.必须要有一个根 2.标签名自定义 3.对 ...

  5. 盒子模型简单理解(box-sizing)

    普通解析: 概念图示和公式: html结构 <div class="num1"></div> 1.只写 width.height(写背景是为了区分) .nu ...

  6. [Unity] Shader(着色器)之纹理贴图

    在Shader中,我们除了可以设定各种光线处理外,还可以增加纹理贴图. 使用 settexture 命令可以为着色器指定纹理. 示例代码: Shader "Sbin/ff2" { ...

  7. safari浏览器在window下 打开控制台

    有时候需要在window下测试safari浏览器的兼容性 然后需要打开错误控制台 以下是完整打开的图文教程 1.显示菜单栏 2.打开偏好设置 3.然后切换到高级标签 勾选 在菜单栏显示开发菜单 4.打 ...

  8. js中join和split的用法

  9. vertx verticle

    以下内容为随手记的,若看客不知鄙人所云,还请原谅则个.............. 公司用的vertx,在国内,这还是款比较年轻的框架,你也可以把他当做一个工具,官网上的说法是: Vert.x is a ...

  10. android学习链接

    Android studio/Gradle学习资源:http://www.cnblogs.com/licheetec/p/4475426.html