The difference between function definition and function expression in JS

Source: Internet
Author: User

Summary: (function () {}) (); and (function () {} ());

2 of the syntax in JavaScript is related to the function keyword:

functions Definition: function functionname (formalparameterlist) {functionbody}

functions expression: function [functionname] (formalparameterlist) {functionbody}

From the definition of syntax, the two are almost identical (the only difference is that the function expression can omit the function name), then, in the case of the interpreter, when the statement of the structure is encountered, is the function expression or function definition?

In the case of JavaScript syntax, if a statement starts with the function keyword, then this paragraph is judged to be a functional definition. A function definition cannot be executed immediately, which undoubtedly leads to a syntax error (SYNTAXERROR), so there must be a way for the parser to recognize it as a function expression.

As already mentioned, the parser recognizes that the condition of the function definition starts with the functions keyword, so naturally, as long as there are any other elements in front of the function keyword, it will be converted from the definition of the functions to function expressions, the following methods are possible, which we all know:

~function () {} ();

!function () {} ();

void function () {} ();

But these methods have one feature, that is, it looks awkward, so up to now, the brackets have been wrapped into a more accepted scheme .

Back to the point, the brackets are also wrapped in 2 ways: (function () {}) (); and (function () {} ());

What they have in common is that they all have brackets. And the parentheses have 2 functions in javascript: The prioritization of operations, and the grouping operators, which are obviously not mathematically or logically calculated from the code, so I think the parentheses here belong to the grouping operator.

By standard, the function of the grouping operator is:

Return The result of evaluating Expression. This could be of type Reference.

Returns the result of evaluating an expression in parentheses. The result may be a reference type.

Aside from words like reference type, one of the key words here should be "evaluation", but there is another important context for grouping operators:

This algorithm does isn't apply GetValue to the result of evaluating Expression.

This algorithm does not use GetValue for the estimated results.

There are a lot of special nouns that look really complicated, in short, using the parentheses operator itself does not allow the code in parentheses to execute immediately, only if the parentheses contain the "grouping" that participates in other operations. Therefore, the statement (function () {}) () is the first to evaluate a function expression with a grouping operator and then participate in the function call. and (function () {} ()) This statement evaluates a function call with the grouping operator and is then executed due to the end of the statement.

The difference between function definition and function expression in JS

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.