Understanding callback function in JavaScript

Source: Internet
Author: User

The following are mainly excerpted from [the]

(1) in JavaScript, functions is first-class objects, which means functions can be used in a first-class manner like object s, since they is in fact objects Themselves:they can is "stored in variables, passed as arguments to functions, created Within functions, and returned from functions ".

(2) Callback functions is derived from a programming paradigm called functional programming. At a simple and fundamental level, functional programming are the use of the functions as arguments. Functional programming Was-and still are, though to a much lesser extent today-seen as an esoteric technique of specially t Rained, master programmers.

(3) When we pass a callback function as an argument to another function, we is only passing the function definition.

(4) If callback function is a asynchronous function[its own defined callback function if an asynchronous function library is called, the function is an asynchronous function; For example: two functions for reading a file in node Fs.readfile () vs Fs.readfilesync ()], then callback function would be executed later than those code behind the function whi Ch called the callback function.

(5) Every function in JavaScript has a methods: Call and Apply .

Callback functions is extremely important in Javascript. They ' re pretty much everywhere. Originally coming from a to traditional C/java background I had trouble with this (and the whole idea of asynchronous PR ogramming), 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 T He call () and apply () functions, or brief code snippits demonstrating their use-so, after learning the hard I decide D-to-try-to-write a simple introduction to callbacks myself.

Functions is objects

To understand callback functions your first has to understand regular functions. This might seen like a "duh" thing to say, but functions in Javascript is a bit odd.

Functions in Javascript is 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, might seem strange (how can code is a string?!) but it ' s actually run -of-the-mill for Javascript. The distinction between code and data is sometimes blurred.

1    //  can create a function by passingthe 2    /  function constructor a string of Code3    varnew Function ("Arg1", "arg2", "return arg1 * ARG2;" ); 4    //  =

One benefit of this function-as-object concept is so you can pass code to another function in the same-a-do you would pas s 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 are easy.

01//define our function with the callback argument02functionsome_function (arg1, Arg2, callback) {03//This generates a random number between04//Arg1 and Arg205varMy_number = Math.ceil (Math.random () *06 (ARG1-ARG2) +arg2);07//Then we ' re do, so we'll call the callback and08//Pass Our result09callback (My_number);10    }11//Call the functionSome_function (5, 15,function(num) {13//This anonymous function would run when the14//callback is calledConsole.log ("Callback called!" +num);16});

It might seem silly to go through all this trouble when the value could just is returned normally, but there is situation s where that ' s impractical and callbacks is 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

Understanding callback function in JavaScript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.