Execute function immediately: (function () {...}) () with (function () {...} ()) What is the difference?

Source: Internet
Author: User

No difference.

function foo() {...}     // 这是定义,Declaration;定义只是让解释器知道其存在,但是不会运行。foo(); // 这是语句,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 a string "function foo(...){}" , it needs to use the parse function, for example, eval() 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 equivalent to:

var foo = function () {...};    // 这就不是定义,而是表达式了。foo();

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

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

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

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 () {    // 这里是真正需要的代码}();

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) {    // 在这里,global 就是全局对象了}(this) // 在浏览器里,this 就是 window 对象

Execute function immediately: (function () {...}) () with (function () {...} ()) What is the 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.