function declaration and function expression--declaration of function Declaration advance

Source: Internet
Author: User
Tags format define object execution expression function definition functions variable

Ways to define functions

There are three main ways to define a function:

Functions Declaration (Function Declaration)
Functional expression function Expression)
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 expression

Typical format for function expressions:

var variable=function (arg1, arg2, ...) {

}

function expression containing name (bracket, function name):

var variable=function functionname (arg1, arg2, ...) {

}

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:

The variable declaration is advanced to the top of the function;
Only the declaration is advanced, initialization is not advance, initialization is also initialized in the original location of initialization;
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 Saytruth () {
Alert (' Myvin is handsome. ');
}

Saytruth ();
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, ...) {

}

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 little friends 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, ...) {

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.