Home > Web Front-end > JS Tutorial > body text

Detailed explanation of JavaScript callback function (callback)

php中世界最好的语言
Release: 2018-03-16 15:47:30
Original
2077 people have browsed it

This time I will bring you a detailed explanation of javscript's callback function(callback), what are the precautions when using javscript callback function (callback), the following is a practical case, let's take a look take a look.

Study the callback function. The English name of the callback function is: callback

Function is also an object. Take a look at the written description:

1. The function uses Function()ConstructorFunction object created.
2. The Function object contains a string , and the string contains the javascript code of the function

Look at a demo1:

  var fun = function (x) {     console.log(x)
   };
   fun(100);   //运行结果:100
Copy after login

The above code As you can know, the function can be an object, and parameters can be passed in the function (such as x in demo1). On the contrary, the parameters in the function can also be passed to a function

demo2:

   function get(x, cb) {        var a = 100;
        a = a + x;
        cb(a)
    }
    get(100, function (a) {        console.log(a)
    });    //运行结果:200
Copy after login

As you can see in demo2, there are two parameters in the function get(), one is x and the other is cb, Among them, cb is a function. You can do something in the function cb()

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Arrary method of JS

##Detailed explanation of JavaScript timer

Events and callback functions of JavaScript running mechanism

The above is the detailed content of Detailed explanation of JavaScript callback function (callback). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template