The class party two days ago, except for eating, drinking, having fun and sleeping nonsense, it was so joyful. It was really better to have fun alone than to have fun.
PS: If you graduate or are about to graduate, you can get together if you have time. After graduation, you will have too little time to get together.
Now I have some time to look at something and summarize something. Because I looked at the JavaScript function part a few minutes ago, I took the time to summarize the relevant parts of the function. Of course, some parts in it are my own understanding. If there is any wrong understanding, please point it out.
In this section, I will talk to you about the statement of function declaration in advance based on my own understanding.
Note: In some places, it is also called function declaration promotion. The translations are different, the meanings are the same, just understand them. Long live understanding!
Before chatting about the declaration of function declaration in advance, it is necessary to introduce several methods of function definition, which most friends should be familiar with. If you understand or don’t want to understand, roll down happily. If you don’t know or want to get familiar with it, slow down and start.
Methods for defining functions
There are three main ways to define functions:
1. Function Declaration
2. Function Expression Function Expression)
3.new Function constructor
Among them, function definition methods for function declarations and function expressions are often used. These two methods have very subtle differences and connections, and the use of these two methods is easy to be confused. Therefore, this article mainly summarizes the relevant knowledge points of these two function definition methods. Of course, the topic of this article is still about functions in advance.
Typical format for function declarations:
function functionName(arg1, arg2, ...){ <!-- function body -->}Function expressions
• Typical format of function expressions:
var variable=function(arg1, arg2, ...){ <!-- function body -->}Function expression containing names (brackets, function names):
var variable=function functionName(arg1, arg2, ...){ <!-- function body -->}Function expressions with names like those above can be used for recursion:
var variable=function functionName(x){ if(x<=1) return 1; else return x*functionName(x);}Statement ahead of schedule
var statement in advance
Friends should have heard of the statement in advance. I would like to reiterate it here, because the statement in advance is an important difference between a function declaration and a function expression, and it is of great significance for us to further understand these two function definition methods.
But before the function declaration is in advance, it is necessary to say that the var declaration is in advance.
First give the early conclusion of the var statement:
Variables are defined in the script or function that declares them, and the variable declaration statement will be advanced to the top of the script or function. However, the variable initialization operation is still executed at the original var statement, and the value of the variable is undefined before the statement is declared.
Three simple points can be summarized from the above conclusion:
1. The variable declaration will be advanced to the top of the function;
2. It is just that the declaration is advanced, the initialization is not advanced, and the initialization is still initialized at the original initialization location;
3. The value of the variable is undefined before declaration.
Let’s take the example:
var handsome='handsome';function handsomeToUgly(){ alert(handsome); var handsome='ugly'; alert(handsome);}handsomeToUgly();The correct output is:
First output undefined, then output ugly.
The error output is:
First output handsome, then output ugly.
This is exactly what variable declaration plays in advance. The handsome local variable is defined in the entire function body. The handsome variable in the function body is suppressed. Oh no, it covers the handsome global variable of the same name, because the variable declaration is advanced, that is, the var handsome is advanced to the top of the function, which is like this:
var handsome='handsome';function handsomeToUgly(){ var handsome; alert(handsome); var handsome='ugly'; alert(handsome);}handsomeToUgly();So before alert(handsome), there is already a var handsome declaration, as mentioned above
The value of the variable is undefined before the declaration
So the first output is undefined.
And because of the above mentioned:
It's just that the declaration is advanced, the initialization is not advanced, the initialization is still initialized at the original initialization location.
So the second output is ugly.
Function declaration in advance
Next two, we will start chatting about the declaration of function declaration in advance in combination with the var declaration.
Friends should be familiar with the declaration of function declaration in advance, so give an example that is very familiar.
sayTruth();<!-- Function declaration-->function saysTruth(){ alert('myvin is handsome.');}sayTruth();<!-- Function expression-->var saysTruth=function(){ alert('myvin is handsome.');}Friends all know that the function definition method of function declaration, that is, the first function calling method above is correct, and the truth that myvin is handsome. can be output, because the function calling statement can be placed after the function declaration. For the function definition method of function expression, that is, the method of the second function call above cannot output the correct result of myvin is handsome.
Combined with the above myvin is handsome. example, the conclusion of the function declaration in advance seems easy to understand. Isn’t it just that when using the function definition method of the function declaration, the function call can be placed anywhere. Yes, you are right, my friend, I don’t know how to refute you. Then let me tell you a few more words.
From what my friend said
Isn't it just that when using function definition methods for function declarations, function calls can be placed anywhere?
It can lead to a little:
When the function declaration is advanced, the function declaration and the function body are advanced.
and:
Function declarations are executed during the pre-execution period, that is, function declarations are executed when the browser is ready to execute code. Because the function declaration is executed during the expected execution period, the function declaration is no longer executed during the execution period (there are people who have already executed it, and they will naturally no longer execute).
Above is a point.
Why can't function expression be declared in advance
Let's talk about one more thing: Why can't function expressions be declared in advance like function declarations?
Fortunately, I know a little, otherwise I really don’t know how I should answer?
Ahem, according to my understanding, let me explain to you:
We said above the var statement in advance, pay attention to what I mentioned above:
It's just that the declaration is advanced, the initialization is not advanced, the initialization is still initialized at the original initialization location.
Ok, let's put the function expression here:
var variable=function(arg1, arg2, ...){ <!-- function body -->}Function expressions are the way to write function definitions into expressions (it seems to be a waste of time, but this is effective in interpreting and understanding that function expressions cannot be declared in advance), which is to assign a function object to a variable, so we write the function expression like this:
var variable=5 Seeing this, maybe friends will understand that one is to assign a value to a variable, and the other is to assign a function object to a variable. Therefore, for function expressions, variable assignment will not be advanced, that is, function(arg1, arg2, ...){<!-- function body -->} will not be advanced, so the function definition is not executed, so function expressions cannot be pre-declared like function declarations.
Analyzing the early instance of function declaration
The same sentence, the reality is from the example:
sayTruth();if(1){ function sayTruth(){alert('myvin is handsome')};}else{ function sayTruth(){alert('myvin is ugly')};}When the browser does not throw an error (please test yourself whether the corresponding browser has thrown an error. Why don’t I test it? Can I say I’m lazy?), the browser’s output result is myvin is ugly (I don’t want to admit it, but the truth is that, ahhhhh, should I read more if I am ugly??????).
Why? Of course, the statement was advanced. Because the function declaration is advanced, the function declaration will be parsed before the code is executed. The execution order is as follows: first parse function saysTruth(){alert('myvin is handsome')}, and when parsing function saysTruth(){alert('myvin is ugly')}, overriding the previous function declaration. When we call the sayTruth() function, that is, during the code execution, the declaration will be ignored, so myvin is ugly will naturally be output (What a cruel reality...). If you forget, you can see what you said above:
Function declarations are executed during the pre-execution period, that is, function declarations are executed when the browser is ready to execute code. Because the function declaration is executed during the expected execution period, the function declaration is no longer executed during the execution period (there are people who have already executed it, and they will naturally no longer execute).
A close call
Let’s talk about functions declared in advance (improvement) here. I hope my understanding and nonsense can be helpful to those in need.
Of course, practice produces true knowledge. The understanding, cognition and application of things still lies in reading, using, and summarizing more. I remember a famous saying that talks about statements and practices: "Let's move and cheer for new statements."
The above brief analysis of function declarations and function expressions - the statement of function declaration is all the content I have shared with you in advance. I hope you can give you a reference and I hope you can support Wulin.com more.