JS Immediate execution Function: (function () {}) and (function () {} ()) What is the difference? _javascript Tips

Source: Internet
Author: User

No difference.

You need to understand the principle of iife, I simply say:

Copy Code code as follows:

function foo () {...} This is the definition, declaration; The definition simply lets the interpreter know it exists, but it does not run.
Foo (); This is the statement that the Statement interpreter encounters the statement that runs it.

Iife is not a must, the traditional one can write this:

Copy Code code as follows:

function foo () {...}
Foo ();

So why Iife?
1. Traditional methods of long-winded, definition and execution are written separately;
2. Traditional methods of directly polluting the global namespace (the browser's global object, such as window)

As a result, developers are looking for a way to solve these problems. So how about writing like this?

function foo (...) {}();

Of course not, but why? Because function foo (...) {} This part is just a declaration, and for the interpreter it's as if you wrote a string "function foo (...) {} ', it needs to use the parse function, such as eval () to execute it. So put () directly behind the declaration is not executed, this is the wrong syntax.

How to get it right? It's easy to say, as long as you turn the declaration into an expression (Expression).

In fact, there are many ways to change the expression, the most common way is to use a pair of function statements wrapped up, so it becomes:

Copy Code code as follows:

(function foo () {...}) This is a deliberately wrapped line, which can actually be connected to the parentheses below.
();

This is equivalent to:

Copy Code code as follows:

var foo = function () {...}; This is not a definition, but an expression.
Foo ();

But the way we said no before, in fact, can also be wrapped directly in parentheses, which is an equivalent expression:

(function foo () {...} ());

So you ask if there's a difference? Very simple: Wood has ~

In addition, there are many ways to change expressions, and there are many other forms of writing, such as:

!function foo () {...} ();

Or

+function foo () {...} ();

These are all OK.

I personally prefer to change the expression with void, because there is no return value for this keyword. But this is really nothing important, just as I "turtle hair" good ...

Copy Code code as follows:

void function () {
Here is the code that really needs
}();

OK, the so-called do not pollute the global namespace, because Iife created a new function scope, your real business code is encapsulated in it, naturally will not touch the global object. If you need a global object, pass it to Iife:

Copy Code code as follows:

void function (Global) {
In this case, global is the Universal object.
} (this)//in the browser, this is the Window object

I have written here a series, one of which is about scope and naming ascension, where the knowledge points are helpful to understanding iife and can continue to read in depth if you are interested: http://www.jb51.net/article/75090.htm

Mode one, call the function, get the return value. Force the function to perform a direct amount of execution and return a reference to the calling execution
Mode two, call the function, get the return value. The force operator makes the function call execute
(function () {}) (); is to parse the function as an expression and then perform the parsed function
Equivalent to var a = function () {}; A (); A gets a function.
(function () {} ()); is to execute the function expression and execution as a statement directly,
Equivalent to var a = function () {} (); A is the result.
The end result is the same,
() is only a function of the self execution
and () There's a lot more.
such as +function () {}
This Equals (function () {}) is generally used (function () {}) and has the function of avoiding global variables

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.