码迷,mamicode.com
首页 > 编程语言 > 详细

理解callback function in javascript

时间:2014-10-14 00:49:27      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   java   for   

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

标签:style   blog   http   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/jiayouwyhit/p/4023187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!