JS Execute function immediately: (function () {...}) () with (function () {...} ()) Is there a difference?

Source: Internet
Author: User

No difference.

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

function foo () {...}     // This is the definition, Declaration; the definition simply lets the interpreter know that it exists, but does not run.  foo ();                    // This is the statement, Statement; The interpreter will run it when it encounters a statement. 

Iife is not a must, the traditional point can be written like this:

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

So why Iife?

    1. Traditional methods of wordy, definition and execution of separate writing;
    2. Traditional methods directly pollute the global namespace (objects in the browser global , such as window )

As a result, developers are looking for a solution to these problems. So, like the following, can you do that?

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

Of course not, but why? Because function foo(...){} This part is just a declaration, as for the interpreter, it's like you're writing "function foo(...){}" A string, it needs to use the eval() parse function, for example, to execute it. So putting () it directly behind the declaration is not going to be executed, which is the wrong syntax.

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

In fact, there are many ways to change expressions, and the most common way to do this is to wrap the function declaration in a pair () , and it becomes:

(function foo () {...})    // This is a deliberate line break, which can actually be linked to the parentheses below. ();

This is equivalent to:

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

But the way we say no, we can actually just wrap it in parentheses, which is also an equivalent expression:

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

So the answer is: wood is different ~

Besides, there are a lot of ways to change expressions, and there are a lot of other things to say, such as:

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

Or

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

These are all possible.

I void have a preference. Use to change the expression, because this keyword does not have a return value. But this really doesn't matter, just when I "turtle hair" good ...

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

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

void function (Global) {    //  Here, Global is the World Object } (this)    // in the browser, this is the Window object

JS Execute function immediately: (function () {...}) () with (function () {...} ()) Is there a difference?

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.