[]javascript function defines the difference between an expression and a function declaration

Source: Internet
Author: User

In JavaScript, functions are defined in two ways, function definition expressions and function declarations, as shown in the following examples:

var test = function (x) {

return x;

}

function test (x) {

return x;

}

Although function definition expressions and Function declaration statements contain the same function names, and both create new function objects, there is a difference between the two.

The function name in a function declaration statement is a variable name, and the variable points to the function object.

The function definition expression and the variable declared through Var, its function is advanced to the top of the script or function, so it is visible within the entire script and/or function. In this case, only the function variable declaration is in advance, and the initialization code of the function is still in its original position. But with a function declaration, the function name and function body are advanced, that is, functions and functions nested within the script are declared before the other code in the current context, or it can be called before a function is declared.

As an example:

Test (1);

function test (x) {

Console.log (x);

}

The above code executes normally and the result output is 1, because the function name and function body are declared in advance for the function declaration statement, which can be called before the declaration.

 

Test (1);

var test = function (x) {

Console.log (x);

}

The above code does not execute properly and will error.

Because for function definition expressions, only the function variable declaration is in advance, but the initialization code of the function is still in its original position, which is equivalent to the following code

var test; function variable declaration in advance

Test (1);

var test = function (x) {

Console.log (x);

}

Therefore, the error test is not a function.

[]javascript function defines the difference between an expression and a function declaration

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.