(function () {}), parsing (function () {} ())

Source: Internet
Author: User

some time before, see (function () {}), (function () {} ()) These functions will be dizzy, do not know what it means, why the outside of the function to add parentheses, after the function of the parentheses, plus and not add exactly what is the difference ... have been confused, read Uncle Tom's "in-depth understanding of the JavaScript series (4): immediately invoke the function expression" only to understand what they are, and finally the things that have plagued me for a long time to kill.

here, let me introduce the differences between function references and function calls, the representation of function declarations, and the representation of function expressions.

the difference between a function reference and a function call

the difference between the function reference and the call is related to whether the function name has parentheses (), the function reference will only appear alone, but the function call must be preceded by a parenthesis, often with an argument attached.

Presentation method (for example):

function foo () {
//function Body
}
var f = foo; Function Reference
var ff = foo (); Function call, you can call Foo () directly, or you can call F ()
Representation of function declarations

Functions with function names are function declarations, including reserved word functions, function names (must have), parameters in parentheses (optional), and function bodies in braces.

Presentation method (for example):

function foo (str) {
//function Body
}
Representation of function expressions

assignment functions, functions with parentheses are function expressions (for example, two examples), including assignment variables (available), function brackets (available), reserved character functions, function names (optional), parameters in parentheses (optional), and function bodies in braces.

Presentation method (for example):

Copy Code
//Assignment-type function expression
var f = function foo (str) {
//function Body
}
//Block bracket-type function expression, which executes the function expression immediately, which is explained later
(function () {
//function Body
})();
Copy Code
Iv. Examples of functions

The first type:

var foo = function () {}
Analysis: The function is a function expression, or it can be understood as a reference, with the Foo () call executable.

The second type:

var foo = function () {} ();
Analysis: The function is called immediately function, which is the difference between the first is the parentheses, that is, function calls and function references, function calls can be understood as self-executing function (preferably in function () {} plus a parenthesis into (function () {}), A little more normative);

The third type:

function () {}
Analysis: The function is missing a name, not assigned value, so error.

The fourth type:

function () {} ()
Analysis: Function () {} is a statement, not a function expression, only the expression can be called, so an error.

The fifth type:

( function () {}) ();
parsing: (function () {}) is a functional expression, which can be called, called an anonymous self-executing function.

The sixth type:

(function () {} ());
Analysis: (function () {} ()) is a functional expression, available, called an anonymous self-executing function (the way Uncle Tom recommended it, I prefer the fifth one).

The seventh type:

function foo () {}
Analysis: This function is a real name function and can be called.

The eighth type:

function foo () {} ();
Analysis: The explanation is the same as the fourth, because function foo () {} is a statement, not an expression, cannot be called, so error.

The nineth type:

function foo () {} (a);
Analysis: The explanation is the same as the fourth, because function foo () {} is a statement, not an expression, cannot be called, but because the arguments are passed in parentheses, the exception is not thrown, but it is not executed by itself.

The tenth type:

(function foo () {});
Analysis: Add a parenthesis outside the function, so the external scope cannot call Foo (), which is used as an anonymous function, I personally think that such a function is meaningless, can not be called or self-executing.

The 11th type:

( function foo () {}) ();
Analysis: With 10, this can be understood as an anonymous self-executing function. But in IE8, the expression is treated as a function declaration, and the function declares a "sticky parsing" behavior, which is declared at the top of the scope, regardless of where the function is defined.

The 12th type:

!function () {} ();
Analysis: In fact, the parentheses and JS &&, XOR, exclamation marks, and so on is the function declaration and expression disambiguation, for the executable.

The 13th type:

new function () {} ();
Analysis: Executable.

Finally, thank you very much. Where to explain the bad or is not the right place, I hope you can feedback to me the first time, and we hope and common progress ~

(function () {}), parsing (function () {} ())

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.