function expressions and Function declarations ~ (own record)

Source: Internet
Author: User

In ECMAScript, the two most common methods of creating a function are function expressions and function declarations, the difference between the two is a bit dizzy, the specification of the ECMA is clear-the function declaration must be marked with an identifier (Identifier) is what everyone says (the function name), The function expression can omit the function name.

Then there is no function name that must be the function expression, then the function name, how to distinguish whether it is a function expression or function declaration ~ ~ ~ If it is part of an assignment expression is a function expression, if it is contained in a function body, or at the top of the program as a function declaration ~

function // declaration, because it is part  of the program var function // expression, because it is part  of an assignment expression New function // expression, because it is a new expression   (function() {    function//  declaration, as it is part of the body of the function   })();

Difference:

The difference between an expression and a declaration is very small-the declaration is preceded by any expression before parsing and calculating ~

Named function expression (function expression with function name)

Referring to a named function expression, it is a matter of course that it has to have a name, the preceding example var bar = function foo () {}; is a valid named function expression, but one thing to remember: This name only works within the scope of the newly defined function, Because the specification specifies that the identifier cannot be valid within the scope of the perimeter:

  var f = function foo () {
Return typeof Foo; Foo is valid within the inner scope
};
Foo is used externally for non-visible
typeof Foo; "Undefined"
f (); "Function"

So what's the use of a named function expression, if so required? Why did you name it?

As we said at the beginning: to give it a name is to make the debugging process more convenient, because when debugging, if each item in the call stack has its own name to describe, then the debugging process is too cool, feel different.

For more information, please see the original page http://www.cnblogs.com/TomXu/archive/2011/12/29/2290308.html

function expressions and Function declarations ~ (own record)

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.