JS callback function Full parsing tutorial

Source: Internet
Author: User

When self-study jquery, saw an English word (Callback), immediately back faint cold sweat. Google quickly found that the original Chinese translation into a callback. That is, the callback function. Do not understand Ah, so in the Google callback function, found that the online Chinese interpretation is too "abstruse", I admit that I caishuxueqian. After looking at some examples of callbacks, it seems a bit understandable. Here is my understanding of the callback function, if the understanding is wrong, please correct, not very grateful.

First of all, from the English definition on the jquery website, as a Chinese, I really feel tragic. The definition of a callback by the domestic "master" interpretation of what, in that circle, it seems that only to put you around, he is a master. Clouds, everything is a cloud.

A callback is a function so is passed as an argument to another function and are executed after it parent function has C ompleted.

This is the explanation of JS, I did not say in other languages.

The literal understanding is that the callback is the process of invoking a function. So let's start by understanding the invocation process. Function A has a parameter, which is a function B, and executes function B after function a finishes executing. Then this process is called a callback.

In fact, Chinese is also very good understanding: callback, callback, is called back to the meaning. Function A is done in advance, and then the function B is called back.

Take a realistic example: after the date you send your girlfriend home, parting, you will certainly say: "Home to give me clockwork information, I am very worried about you." "No, then your girlfriend really sent you a message when she came home. Boy, you have a chance.

In fact, this is the process of a callback. You left a function B (ask your girlfriend to give you A clockwork message) to your girlfriend, and then your girlfriend goes home, and the action to go home is function A. She had to get home first, the content of function A is finished, then function B is executed, and then you receive a message.

It is important to be clear here: function B is passed to function A as a parameter, then function B is called a callback function.

Perhaps some people have doubts: must be passed in the form of parameters, I can not directly in function A call function B? I can do that. In the solution.

<: If you call it directly in function A, the callback function is limited to death. But using the function to do the argument has the following advantages: When you have a (b) function B is a callback function, and you can also a (c) this time, function c is a callback function. If you write function a () {...; b ();} The flexibility of the variable is lost. >

The following code is used to confirm my understanding.

[HTML]View Plaincopy
  1. <html>
  2. <head>
  3. <title> Callback Function (callback)</title>
  4. <script language="javascript" type="Text/javascript">
  5. Function A (callback)
  6. {
  7. Alert ("I am the parent function A! ");
  8. Alert ("Call callback function");
  9. Callback ();
  10. }
  11. Function B () {
  12. Alert ("I am a callback function B");
  13. }
  14. Function C () {
  15. Alert ("I am the callback function C");
  16. }
  17. function test ()
  18. {
  19. A (b);
  20. A (c);
  21. }
  22. </Script>
  23. </head>
  24. <body>
  25. <H1> Learn JS callback function </h1>
  26. <button onclick=test ()>click me</button>
  27. <P> should be able to see two callback functions called </P>
  28. </body>
  29. </html>

Original address: http://blog.csdn.net/lulei9876/article/details/8494337

JS callback function Full parsing tutorial

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.