Description of functions and function expressions-advance declaration of function declaration

Source: Internet
Author: User

Description of functions and function expressions-advance declaration of function declaration

Two days ago, in addition to eating, drinking, and having fun, the class party went to bed. It was so joyful.

PS: If you have graduated or are about to graduate, you can get together when you have time. After finishing the course, you will have less time to get together.

Now I have a little time to look at some things and summarize some things. I also looked at the JavaScript function part due to the broken parts of the slice some time ago. So I took the time to summarize the relevant parts of the function. Of course, some of them are their own understandings. If you have any misunderstanding, please kindly advise them.

In this section, I will talk with my friends about the declaration of function declaration in advance based on my own understanding.

Note: In some cases, function declaration is promoted. Translation is not the same, meaning is the same, everyone can understand it. Long live!

Before talking about the declaration of function declaration, it is necessary to introduce several methods of function definition. Most of our friends should be familiar with it. If you know something or don't want to know it, roll it down. If you are not familiar with it or want to be familiar with it, you can start slowly.

Define a function

There are three methods to define functions:

1. Function Declaration)
2. Function Expression)
3. new Function Constructor
Among them, function declarations and function definition methods of function expressions are often used. These two methods have subtle differences and relationships, and they are easy to be confused, therefore, this article mainly summarizes the knowledge points related to these two function definition methods. Of course, the topic of this article is still about functions in advance.

Typical format of function declaration:

function functionName(arg1, arg2, ...){  <!-- function body -->}

Function expression

• Typical format of function expressions:

var variable=function(arg1, arg2, ...){      <!-- function body -->}

The function expression that contains the name (ARC, function name:

var variable=function functionName(arg1, arg2, ...){    <!-- function body -->}

Like the above named function expression, it can be used for recursion:

var variable=function functionName(x){    if(x<=1)      return 1;    else      return x*functionName(x);}

Advance declaration

Var statement advance

Everyone should have heard of the statement in advance. I want to repeat it here, because advance declaration is an important difference between function declaration and function expression, it is of great significance for us to further understand the two function definition methods.

But before the function declaration is made, it is necessary to say that the var declaration is made in advance.

First, we will give the early conclusion of the var statement:

Variables are defined in the scripts or functions that declare them. The variable declaration statement is advanced to the top of the script or function. However, the initialization of the variable is still executed at the position of the original var statement, and the value of the variable is undefined before the statement is declared.

The above conclusions can summarize three simple points:

1. The variable declaration is advanced to the top of the function;
2. The statement is declared to be in advance, Initialization is not in advance, and Initialization is still performed at the original initialization position;
3. Before declaration, the variable value is undefined.

Here is an example:

var handsome='handsome';function handsomeToUgly(){  alert(handsome);  var handsome='ugly';  alert(handsome);}handsomeToUgly();

The correct output result is:
Output undefined first, and then uugly.

The error output result is:
Output handsome first and then uugly.

Here is the role of variable declaration in advance. The handsome local variable is defined in the whole function body, and the handsome variable in the function body is pressed. Oh no, it overwrites the handsome global variable with the same name, because the variable declaration is in advance, that is, var handsome is pushed 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();

Therefore, before alert (handsome), we already have the var handsome declaration

Before declaration, the variable value is undefined.
So the first output is undefined.

And because the above mentioned:

The statement is declared to be in advance, Initialization is not in advance, and Initialization is still performed at the original initialization location.
Therefore, the second output is uugly.

Function declaration in advance
Next, let's talk about the declaration of the function declaration in advance in combination with the var declaration.

The declaration of function declaration should be familiar to our friends in advance. For example, we are not familiar with the Declaration.

SayTruth (); <! -- Function declaration --> function sayTruth () {alert ('myvin is handsome. ');} sayTruth (); <! -- Function expression --> var sayTruth = function () {alert ('myvin is handsome .');}

As we all know, the function definition method of the function declaration is correct, that is, the first function call method above can output myvin is handsome. because the function call statement can be placed after the function declaration. For the function definition method of the function expression, that is, the method called by the second function above cannot output the correct result of myvin is handsome.

In combination with the above myvin is handsome. example, the early conclusions of function declaration seem to be well understood, not that function calls can be placed anywhere when the function definition method of function declaration is used. Yes, you are right. I don't even know how to refute you, my friend. Let me talk a few more words.

From what my friends say

It is not that function calls can be placed anywhere when the function definition method declared by the function is used.
One point can be introduced:

When the function declaration is in advance, the function declaration and function body are both in advance.

And:

The function declaration is executed during the pre-execution period, that is, the function declaration is executed when the browser is preparing to execute the code. Because the function declaration is executed during the pre-execution period, when the execution period is reached, the function declaration will no longer be executed (if all the statements are executed, they will not be executed again ).

The above is a bit.

Why can't function expressions be declared in advance?
Let's talk about another point: Why can't function expressions be declared in advance like function declaration?

I know something about Xin Kui. Otherwise I really don't know how to answer it?

Ke, according to my understanding, explained to my friends:

We mentioned the var statement above in advance. Note the above mentioned:

The statement is declared to be in advance, Initialization is not in advance, and Initialization is still performed at the original initialization location.

OK. Let's look at the function expression here:

var variable=function(arg1, arg2, ...){          <!-- function body -->}

A function expression is a way to write a function definition into an expression (it seems to be white, but it is helpful to explain and understand that a Mao function expression cannot be declared as a function ), it is to assign a function object to a variable, So we write the function expression like this:

Var varible = 5. you may understand this. One is to assign a value to a variable, and the other is to assign a function object to a variable. Therefore, for a function expression, variable assignment is not advanced, that is, function (arg1, arg2 ,...) {<! -- Function body -->} is not implemented in advance, so the function definition is not executed. Therefore, function expressions cannot be declared in advance like function declaration.

Function declaration instance analysis in advance

Let's look at the following 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 whether the corresponding browser throws the error yourself. Why don't I test it? Can I say I'm lazy ...), The output result of the browser is myvin is uugly (I don't want to admit it, but this is the case. Do you have to read more books if you are ugly ??????).

Why? Of course, the statement is in advance. Because the function declaration is in advance, the function declaration will be parsed before the code is executed. The execution order is like this. First, the function sayTruth () {alert ('myvin is handsome')} is parsed ')}, parsing function sayTruth () {alert ('myvin is ugg')} overwrites the previous function declaration. When we call the sayTruth () function, that is, when the code is executed, the declaration will be ignored, so myvin is uugly (cruel reality...) will naturally be output ...). If you forget it, you can see the following:

The function declaration is executed during the pre-execution period, that is, the function declaration is executed when the browser is preparing to execute the code. Because the function declaration is executed during the pre-execution period, when the execution period is reached, the function declaration will no longer be executed (if all the statements are executed, they will not be executed again ).

A knot is missing.

Let's talk about the function declaration in advance (upgraded). I hope that my understanding and comments will be helpful to those who need it.

Of course, practice is true. The understanding, cognition, and application of things are still based on reading more and summarizing things. Remember the famous saying that the explanation and practice are: "Let's get up and cheer for the new statement .".

The above analysis of the Function Description and function expression-the Declaration of the function declaration is all the content that I have shared with you in advance. I hope you can give us a reference and support the help house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.