Differences between function expressions and function declaration in JavaScript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the function expressions and function declarations in JavaScript and the different information about function declarations and function expressions. For more information, see Function expressions and function declarations

In ECMAScript, the two most common methods for creating a function are function expressions and function declarations. The difference between the two methods is a bit dizzy, because the ECMA specification only makes it clear: the function declaration must contain an Identifier (the name of a function that is commonly used), while the function expression can omit this Identifier:

  Function declaration:

Function Name (parameter: OPTIONAL) {function body}

  Function expression:

Function Name (optional) (parameter: OPTIONAL) {function body}

Therefore, we can see that if the function name is not declared, it must be an expression. If the function name is declared, how can we determine whether it is a function declaration or a function expression? ECMAScript is distinguished by context. If function foo () {} is a part of the value assignment expression, it is a function expression. If function foo () {} is contained in a function, or at the top of the program, it is a function declaration.

Function foo () {}// declaration, because it is part of the program var bar = function foo () {}; // expression, because it is a part of the value assignment expression new function bar () {}; // expression, because it is a new expression (function () {function bar () {}// declaration, because it is part of the function body })();

Another function expression is not very common, that is, function foo () {}) enclosed by brackets. It is the reason for the expression because parentheses () are a grouping operator, it can only contain expressions. Let's look at several examples:

Function foo () {}// function declaration
(Function foo () {}); // function expression: Included in the grouping Operator

Name function expression

When it comes to the naming function expression, it must have a name. In the preceding example, var bar = function foo () {}; is a valid naming function expression, but remember one thing: this name is valid only in the scope of the newly defined function, because the specification stipulates that the identifier cannot be valid in the peripheral scope:

Var f = function foo () {return typeof foo; // foo is valid within the internal scope}; // foo is used externally as an invisible console. log (typeof foo); // "undefined" console. log (f (); // "function" var f = function foo () {return foo; // foo is valid within the internal scope }; // foo is used externally as an invisible console. log (typeof foo); // "undefined" console. log (f () = f); // "function" console. log (f. name); // foo

In this case, what is the use of the name function expression? Why?

As we said at the beginning: to give it a name is to make the debugging process more convenient, because during debugging, if each item in the call Stack has its own name, the debugging process is so cool that you may feel different.

Ps: differences between function declaration and function expression in JS

The function declaration in Js refers to the following form:

function functionName(){ } 

A function is declared in this way, while a function expression declares a function like an expression, for example:

     var functionName = function(){ }

Many of my friends may be confused when they see these two types of writing. These two types of writing are similar and seem to be feasible in the application. What are their differences?

In fact, the js parser does not treat function declaration and function expressions equally. For function declaration, the js parser reads the statements first to ensure that the Declaration has been parsed before all code is executed, and the function expression is like defining other basic types of variables, it will be parsed only when a sentence is executed, so in practice, they will still be different, as shown in, when defining a function in the form of a function declaration, you can write the call statement before the function declaration, and the latter will report an error.

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.