Callback functions in JavaScript

Source: Internet
Author: User
Tags setinterval

  In the process of learning JavaScript encountered a lot, using the callback function example, there are a lot of questions, the beginning of a chestnut:

The first callback function that comes into contact with JavaScript is in SetInterval () and settimeout ():

1   var num = ten; 2 3   var intervalid = setinterval (function  () {4      console.log (num); 5       num--; 6       if (num==0) {7        clearinterval (intervalid); 8      }9  },1000);

  The code implements the use of SetInterval () to print the value in the console and then stop printing when the value becomes 0.

When using SetInterval (), an anonymous function was used as the callback function, and when I saw this, I lost it.

This anonymous function has no argument list, that is, this is a function declaration, not a call, and this anonymous function is written in the setinterval () parameter list position, indicating that it is the formal parameter of the setinterval (), but when is this function called? If this function also needs to pass the argument, how to implement the parameter passing when the anonymous function is called?

  

  Once again, it is time to use AddEventListener () for event binding in the Learning DOM2:

  

1 btn2.addeventlistener ("click",function  () {2     btn1.removeeventlistener (" Click ", Func1)3 },false)

  When canceling an event binding, there is a requirement that the callback function in Removereventlistener () cannot be an anonymous function. But the problem is still unresolved, and then the callback function of the tangible parameter list is encountered:

The sort function in the array: sort ();

1         Arr3.sort (A, b) {2             //return a-B;//B. Before, a in the back (ascending order)3              return b-a;  // A in front, b in the back (descending order) 4         });

This time the callback function has a formal parameter, but the question is still the question, where is the formal parameter, a, and when it becomes an argument? When they are given arguments, what is the value of the assignment?

All possibilities are excluded, and only one is that thefunction of sort () assigns values to the parameters of the callback function and invokes the callback function, which requires that the function declaration of the callback function be written according to the function that called it, that is Call its function needs it to complete what kind of function, the function body of the callback function should be what , and its parameters can not be confused, must be written in accordance with the provisions of the formal parameters, he in the call, the passing of several arguments, it is necessary to write a few formal parameters.

When writing callback functions in a function written by the system, the part that the programmer needs to complete is just the declaration part of the callback function, and the parameters must be written in accordance with the rules.

Functions are also objects

To figure out the callback function, you first have a clear understanding of the rules of the function. In JavaScript, the function is rather strange, but it is indeed an object. To be precise, a function object is created with a function () constructor. The function object contains a string that contains the JavaScript code for the functions. How can the code be a string? For JavaScript, this is common. The difference between the data and the code is very vague.

 A callback function is a function that is called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.

Therefore, callbacks are essentially a design pattern, and the design principles of jquery (including other frameworks) follow this pattern.

In JavaScript, the callback function is specifically defined as: function A is passed as a parameter (function reference) to another function B, and this function B executes function A. Let's just say that function A is called a callback function. If there is no Name (function expression), it is called an anonymous callback function.

So callback is not necessarily used for asynchrony, and callbacks are often used in scenarios where general synchronization (blocking) is required, such as executing a callback function after certain operations have been performed.

Callback functions 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.