On the JavaScript callback function


function

Before reading this article you should already know, what is the function in JavaScript. If you do not know what is the function, then make way check wailing. (● 'ω` ●)

How to declare a JavaScript function?

Now let me give a simple request: to write a function that can print out hello world.
I give below three ways to declare function.
Here Insert Picture Description

Function call

You know the function declaration, that function how to call it?
Now let me give a simple request: call a function, print out hello world.
Method One : The declaration is a normal function, and then 函数名()call.
Here Insert Picture Description
Method two anonymous calls from a function
like declaring a variable, a function declaration, this time you put the variable name crossed out, it does not know who it is it, this is an anonymous function .
Here Insert Picture Description
How to call the anonymous function?
(1 • ㅁ • ฅ) I call the run, but you can not hide. The function name you did not, then I'll have your whole body function into parentheses, then add parentheses behind the call. It is(匿名函数的函数体)()
Here Insert Picture Description

Callback

What is a callback function?

  • Mozilla community is to say: to be as a function of arguments passed to another function, and is called in the external function, with its complete certain tasks, called the callback function.
  • 再看一个英文释义: A callback is a function that is passed as an argument to another function and is executed after its parent function has completed。

Simple to understand: the callback function that will be a function (function parameters) as an argument passed to another function (main function) inside, and the parameter called callback function.

For chestnuts

  • I declare a function Greeting, it needs to accept a function as a parameter.
    • I declare a greet function.
    • Greeting(greet)When executed, callsgreeting()Here Insert Picture Description
  • Put another wording: Like the top of logic
    • But I greet changed anonymous function.
    • Greeting(匿名函数)When executed, calls 匿名函数
      Here Insert Picture Description
      this view will not be clearer, it is clear that this is an anonymous function right.
      Here Insert Picture Description

This is the top of the sync callback For example, it is often the callback function for performing asynchronous operations, that is called asynchronous callback. Of course, this is something of interest to themselves to find information. Analysis of here with just a callback function only.

Callback function parameters

Really a question: callback function parameters of what is?
You would certainly say, it is not a function chant.
I used to feel very simple question ...... but tonight I callback function + settimeout + promise + async after mixing up the mess I actually confused.
Here I am serious statement!
Callback function parameter list :

  • Either write the name of the function ! ! ! Without parentheses! ! !
  • Either write the body of the function ! ! ! Complete an anonymous function! ! !
    Callback function performs the function of the main body :
  • Write函数名()

Check out my personal demonstration! ! ! Tragic history! ! !
Here Insert Picture Description
Here Insert Picture Description
Come talk about why the lines 29-31 and line equivalent of 32!

This is the anonymous function to perform this function after two seconds, which is executed resolve, because the resolve in the body of the function, the brackets have to be executed! ! !
Here Insert Picture Description
This is executed resolve after two seconds, resolve in the parameter list, just write the function name on the list! ! !


Well, I Luo Lian, this paper shows that I am a fool (╯¯Д¯) ╯╘═╛

Published 131 original articles · won praise 451 · views 540 000 +

Guess you like

Origin blog.csdn.net/qq_36667170/article/details/105149708