function declaration and function expression--declaration of function declaration in advance

Source: Internet
Author: User

Written in front of those have to say crap

The first two days of the class party, in addition to eating and sleeping is to sleep, it is joy, really alone lele than the public Lele ah.

PS: Graduating or graduating will have time to gather together, after the industry belongs to their own time to get together can be less too much.

Now a little time to see something summed up something, and because the previous time slices mana to see the function part of JavaScript, so take the time to summarize the relevant parts of the function, of course, some of the parts are their own understanding, if there is no understanding of the wrong place also please small friends to point out.

In this section I combine my understanding with the small partners to talk about the declaration of a function in advance .

Note: Some places are also called function declaration elevation. The translation is different, the meaning is the same, everyone understands. Understanding Hooray!

Before the declaration of the function declaration is advanced , it is necessary to introduce several methods of function definition, and most of the small partners should be familiar. Know or do not want to know a happy roller roll down, unfamiliar or want to be familiar with the slow pace of the start to walk.

Methods for defining functions

There are three main ways to define a function:

    1. Functions declaration (Function Declaration)
    2. function expression)
    3. New function constructor

Among them, the function declaration and function expression function definition method is often used, the two methods have very subtle difference and connection, and the use of the two methods is easy to confuse, so this article mainly summarizes the two function definition methods related knowledge points, of course, the topic of this article is still about the function of advance.

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 -->}
  • A function expression containing the name (parentheses, function name):

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

    A function expression with a name like above can be used recursively:

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

The small partners should have heard the statement in advance , and I would like to reiterate it again, because declaration is an important difference between function declaration and function expression in advance, which is of great significance for us to further understand these two methods of function definition.

But again before the function declaration is advanced , it is necessary to say the var declaration in advance .

First give var the conclusion that the declaration is in advance :

Variables are defined in the script or function that declares them, and 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 before the statement is undefined.

The above conclusions can be summed up in three simple points:

    1. The variable declaration is advanced to the top of the function;
    2. Only the declaration is advanced, initialization is not advanced, initialization is also initialized in the original location;
    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 first undefined , then output ugly .

The output of the error is:
Output first handsome , then output ugly .

This is where variable declarations come in advance . The handsome local variable is defined throughout the function body, and the variable in the body of the function is handsome pressed, oh no, the global variable with the same name is overridden, handsome because the variable declaration is advanced to the top of the function, which var handsome is what it looks like:

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

So alert(handsome) before, there has been a var handsome statement, by the above mentioned

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 at the original location.

So a second output ugly .

function declaration in advance

We'll start by combining the declaration var of a function declaration in advance .

Declaration of function declaration in advance the small partners should be familiar with, and give a familiar example.

sayTruth();<!-- 函数声明 -->function sayTruth(){    alert(‘myvin is handsome.‘);}sayTruth();<!-- 函数表达式 -->var sayTruth=function(){    alert(‘myvin is handsome.‘);}

As the small partners know, the function definition method for the function declaration, that is, the first function call method above is correct, can output myvin is handsome. the truth, because the function call statement can be placed after the function declaration. The function definition method for the function expression, that is, the method of the second function call above, cannot output myvin is handsome. the correct result.

In conjunction with the above myvin is handsome. example, the function declaration in advance seems to be well understood, not just when a function-declaration method is used to define a function, the function call can be placed in any position. Yes, you're right, little buddy, I don't know how to refute you. Then let me just say a few more words.

From what the little Buddy said.

The function call can be placed anywhere in the function definition method using function declaration.

Can elicit a point:

The function declaration and function body are in advance when the function declaration is in advance.

And:

function declarations are performed during the pre-execution period, meaning that the function declaration is performed when the browser is ready to execute the code. Since the function declaration is executed during the pre-execution period, the function declaration is no longer executed when the execution period is performed (the person has performed the nature and no longer executes).

Above is a point.

Why can't a function expression declare an advance

Let's say one more thing: why can't a function expression advance a function declaration like a function declaration?

I know a little, or I really do not know how to answer it?

Cough, according to my understanding to the small friends explained below:

We said above the var statement in advance, notice what I mentioned above:

Only the declaration is advanced, initialization is not advanced, initialization is also initialized at the original location.

Ok, let's look at the function expression here:

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

A function expression is a way of writing a function definition into an expression (seemingly white, but this is good for interpreting and understanding that the expression of a hair function cannot be declared in advance), which is to assign a function object to a variable , so we write the function expression this way:

var varible=5

See this, maybe the little friends will understand, one is to assign a value to a variable, one is to assign a function object to a variable, so for the function expression, the variable assignment is not advanced, that function(arg1, arg2, ...){<!-- function body -->} is, the function definition is not executed, So function expressions cannot be declared in advance as function declarations do.

Instance analysis in advance of function declaration

Or that sentence, or an example of the reality:

sayTruth();if(1){    function sayTruth(){alert(‘myvin is handsome‘)};}else{ function sayTruth(){alert(‘myvin is ugly‘)};}

In the case that the browser does not throw an error (please test the appropriate browser if there is a throw error, why I do not test?) Can I say I'm lazy ... ), the output of the browser is output myvin is ugly (I do not want to admit, but the fact is that AH ah ah ah, do not say that people are ugly should read more books?????? ).

Why is it? Of course the declaration was advanced. Because the function declaration is in advance, the function declaration is parsed before the code executes, the order of execution is, parsing, function sayTruth(){alert(‘myvin is handsome‘)} parsing function sayTruth(){alert(‘myvin is ugly‘)} , overwriting the previous function declaration, when we call the function, that is, during the execution of the code, the sayTruth() declaration is ignored, so it will naturally output myvin is ugly(a good cruel reality ...) )。 Forget to look at the above mentioned:

function declarations are performed during the pre-execution period, meaning that the function declaration is performed when the browser is ready to execute the code. Since the function declaration is executed during the pre-execution period, the function declaration is no longer executed when the execution period is performed (the person has performed the nature and no longer executes).

A little knot.

function declaration functions in advance (Ascension) just talk about it here, and hopefully my understanding and nonsense will help the small partners in need.

Of course, practice is a real truth. The understanding of things, cognition and use of more than to see more use more summary, remember there is a famous saying, is to say statements and Practice: "Move up, cheer for the new statement ." ”。

function declaration and function expression--declaration of function declaration in advance

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.