• 理解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/

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

  • 相关阅读:
    将数组转换为 List, 使用 Collections.addAll(arrayList, array)
    Numpy学习笔记
    sql 批量修改字段内容
    sql 查询的优化
    选择低薪喜欢的工作, 还是高薪不喜欢的工作 ?
    Tornado + Bootstrap 快速搭建自己的web应用
    sql查询出现次数最多的记录的名称和现次数以及QT聚合查找失败解决
    idea中修改git提交代码的用户名
    初识Spring Cloud与微服务
    微信小程序解析富文本的几种方法
  • 原文地址:https://www.cnblogs.com/jiayouwyhit/p/4023187.html
Copyright © 2020-2023  润新知