Article Directory
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.
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.
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 .
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(匿名函数的函数体)()
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()
- Put another wording: Like the top of logic
- But I greet changed anonymous function.
Greeting(匿名函数)
When executed, calls匿名函数
this view will not be clearer, it is clear that this is an anonymous function right.
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! ! !
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! ! !
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 (╯¯Д¯) ╯╘═╛