js callback function (callback) (reprint)

Learning jquery, callback function feeling very confused, at night looking for a long time, suddenly found this article very plain, basic illustrates the problem. It is reproduced

original:

Self jquery, to see a English word (Callback), suddenly back faint cold sweat. The rapid google and found that the Chinese translation into callback. That is a callback function. I do not know ah, so in google callback function, found online Chinese explanation is too "esoteric" I admit the ignorant. After reading a few examples of callbacks, it looks like a little understanding. Here is my understanding of the callback function, and if it wrong, please correct me, appreciate it.

        First is the jquery start English definition from the site, as a people, I really feel the tragedy. A callback definition is domestic "experts" to explain what, in that circle, seemingly only to you go around, and he was considered a master. Clouds, all clouds.

A callback is a function that is passed as an argument to another function and is executed after its parent function has completed.

This is explained in the js, I did not count other languages.

Literally is down, the callback procedure is to call a function. From then understand the calling process started. Has a function of a parameter, the parameter B is a function, a function When executing the function performed b. Then this process is called a callback.

In fact, Chinese is also well understood: callback, callback, call back is the meaning. A function of pre-finished, go back and call the function b.

For realistic example: After the date you sent your girlfriend to go home, parting, you would certainly say:. "I got home to the additional information, I'm worried about you," do not, then after your girlfriend back home really sent you a message. Boy, Me and you.

In fact, this is a callback procedure. You left a function b (required girlfriend to give you additional information) to your girlfriend, your girlfriend and then go home, go home action is a function of a. After she must go home, the contents of a function execution is over, and then execute the function b, then you will receive a piece of information.

Here it must be clear: b is a function you pass a function as an argument, then the function b is called the callback function.

 Some people may have doubts: Be sure to pass it as a parameter in the past, I can not call a function directly in the function of a b there? Indeed. Solving.

<FAQ: If you call a function directly in the case where, then the callback function is limited to dead. But the benefits of using functions as parameters have the following: When you're a (b) when it became a callback function b, and you can a (c) This time, became a callback function c. If you write a function a () {...; b ();} loses flexibility variables. >

Use the following code to confirm my understanding.

 

 

< HTML >  
 
< head >  
 
< title > callback (the callback) </ title >  
< Script Language = "JavaScript" type = "text / JavaScript" >  
function A (the callback) 
{     
    Alert ( " I am a parent function A! " ) ; 
    Alert ( " callback function " ); 
    callback (); 
} 
function b () { 
Alert ( " I am a callback function b " );
 
} 
function c(){ 
alert("I is a callback function C " ); 
 
} 
 
function Test () 
{ 
    A (B); 
   A (C); 
} 
 
</ Script >  
</ head >  
 
< body >  
< h1 of > Learning js callback </ h1 of >  
< Button the onClick the Test = () > the Click Me </ the Button >  
< the p- > should see two callback function called </ the p- >  
</ body >  
 
</ HTML > 

 

Guess you like

Origin www.cnblogs.com/luowei/p/11692494.html