Two days before the class party, in addition to eat and drink is sleeping, is a joy, is really a lonely lele than the public Lele ah.
PS: Graduated or about to graduate have time to get together, after the industry belongs to their own time to get together can be too much less.
Now a little time to look at something and sum up something, and because the previous period of time slices definitely look at the function of JavaScript, so draw time to summarize the relevant parts of the function, of course, there are some parts are their own understanding, if there is a wrong understanding of the small partners are not hesitate to point out.
In this section I combine my understanding with my little friends to talk about the declaration of functions in advance.
Note: Some places are also called function declaration elevation. Translation is not the same, meaning the same, we understand the line. Understand Long live!
Before declaring the declaration of a function, it is necessary to introduce several methods of defining the function, most of the small partners should be familiar. Know or do not want to know the happy roll down the scroll, unfamiliar or want to be familiar with the slow pace of start to go.
Ways to define functions
There are three main ways to define a function:
1. function declaration (Functions Declaration)
2. Functional expression function Expression)
3.new function Constructor
Among them, function declarations and function expressions are often used to define methods, these two methods have very subtle differences and connections, and the use of these two methods is also easy to confuse, so this article mainly summarizes the two methods of function definition related knowledge, of course, the theme of this article is still about the function ahead.
Typical format for function declarations:
function functionname (arg1, arg2, ...) {
<!--function Body-->
}
function expression
• Typical format for function expressions:
var variable=function (arg1, arg2, ...) {
<!--function Body-->
}
function expression containing name (bracket, function name):
var variable=function functionname (arg1, arg2, ...) {
<!--function Body-->
}
A function expression with a name like the above can be used recursively:
var variable=function functionname (x) {
if (x<=1) return
1;
else return
x*functionname (x);
}
Declaration Advance
VAR declaration advance
Small partners should have heard the statement ahead of time, I would like to reiterate here again, because the declaration of advance is a function declaration and function expression of an important difference, for us to further understand the two methods of function definition is of great significance.
But again, before the function statement is ahead, it is necessary to say the Var statement ahead of time.
First, give the conclusion of the Var statement in advance:
Variables are defined in the script or function that declares them, and the variable declaration statements are advanced to the top of the script or function. However, the operation of the variable initialization is performed at the location of the original VAR statement, and the value of the variable is undefined before the statement is declared.
The above conclusions can be summed up in three simple points:
1. The variable declaration will advance to the top of the function;
2. Only the declaration is advanced, initialization is not advance, initialization is also initialized in the original location of the initialization;
3. The value of the variable before the declaration is undefined.
Here are some examples:
var handsome= ' handsome ';
function handsometougly () {
alert (handsome);
var handsome= ' ugly ';
alert (handsome);
}
Handsometougly ();
The correct output results are:
Output undefined first, then output ugly.
The error output is:
Output handsome first, then output ugly.
Here is the effect of the variable declaration in advance. The handsome local variable is defined throughout the function, and the handsome variable in the function is pressed, oh no, it covers the handsome global variable with the same name, because the variable declaration is ahead, that is, the Var handsome is advanced to the top of the function:
var handsome= ' handsome ';
function handsometougly () {
var handsome;
alert (handsome);
var handsome= ' ugly ';
alert (handsome);
}
Handsometougly ();
So before alert (handsome), there was a var handsome statement, which was mentioned above
The value of the variable before the declaration is undefined
So the first output undefined.
And because of the above mentioned:
Only the declaration is advanced, initialization is not advanced, initialization is also initialized in the original location of the initialization
So the second output ugly.
function declaration Advance
Then we'll start with the Var declaration in advance of the declaration of the function declaration.
Declaration of functions in advance small partners should be very familiar with, for example, familiar.
Saytruth ();<!--function declares--> functions
Saytruth () {
alert (' Myvin is handsome. ');
}
Saytruth ();<!--function Expression-->
var saytruth=function () {
alert (' Myvin is handsome. ');
}
As the partners know, the function-definition method of the function declaration, the first function invocation method above, is correct and can output the truth of the Myvin is handsome, because the function call statement can be placed after the function declaration. For the function definition method of the function expression, the second function call method above cannot output the correct result of the Myvin is handsome.
In conjunction with the Myvin is handsome above, the conclusion of the function declaration in advance seems to be well understood, not that function calls can be placed in any position when using function-declared functions to define methods. Yes, you're right, little buddy, I don't know how to contradict you. Let me just say a few more words.
From what the little Buddy said.
It's not just that when you use function-declaration functions to define a method, a function call can be put anywhere.
Can draw a point:
The function declaration and function body are both advanced in advance of the function declaration.
And:
Function declarations are executed during the execution period, meaning that the function declaration is executed when the browser is ready to execute the code. Because the function declaration is executed during the execution period, the function declaration is no longer executed (when people have done it, they will not execute it).
It's a little above.
Why can't a function expression declare an advance
Let's say one more thing: why can't a function expression behave like a function declaration ahead of time?
I know a little of sin, or I don't know how to answer it.
Cough, explain to my friends in my understanding:
We said above the Var statement ahead of time, notice what I mentioned above:
Only the declaration is advanced, initialization is not advanced, initialization is also initialized in the original location of the initialization
Ok, let's look at the function expression here:
var variable=function (arg1, arg2, ...) {
<!--function Body-->
}
A function expression is the way in which a function is defined to be written as an expression (seemingly white. But this is to explain and understand that the expression of the wool function can not be a function of the declaration of good results in advance is to assign a function object to a variable, so we write the function expression in this way:
var varible=5 see this, perhaps the small partners will understand, one is to assign a value to a variable, one is to assign the function object to a variable, so for function expression, variable assignment is not advance, that is function (Arg1, arg2, ...) {<!--function body is not ahead of time, so functional definitions are not executed, so function expressions cannot be advanced in function declarations like function declarations.
An example analysis of function declaration advance
Or that sentence, or the example of the truth:
Saytruth ();
if (1) {
function Saytruth () {alert (' Myvin is Handsome ')}
else{
function Saytruth () {alert (' Myvin is Ugly ')};
}
In case the browser does not throw errors (please test yourself if the browser is throwing errors, why don't I test?) Can I say I'm lazy ... ), the output of the browser is the output Myvin is ugly (I do not want to admit, but the fact is that AH ah ah ah, the word is ugly people should read more?????? )。
Why, then? Of course the statement was advanced. Because the function declaration is in advance, the function declaration resolves before the code executes in such a way that it resolves function saytruth () {alert (' Myvin is Handsome ')} before parsing function Saytruth () { Alert (' Myvin is Ugly ')}, overwriting the previous function declaration, when we call the Saytruth () function, that is, the declaration is ignored during the execution of the code, so it will naturally output Myvin is ugly (good brutal reality ...). )。 Forget to read the above said:
Function declarations are executed during the execution period, meaning that the function declaration is executed when the browser is ready to execute the code. Because the function declaration is executed in the pre-execution period, the function declaration is no longer executed at the execution time (the person who has done so naturally will no longer execute it).
It's a little knot.
about function declaration functions in advance (promotion) I want to talk about here first, I hope my understanding and nonsense can help the small partners in need.
Of course, practice is true. The understanding of things, cognition and use is to see more use more summary, remember a famous saying, is to speak the Declaration and practice: "Move up, for the new declaration of applause." ”。
The above analysis function declaration and function expression--function declaration advance is small series to share all the content, hope to give you a reference, also hope that we support cloud habitat community.