Immediately execute functions and Functions

js execute function immediately: (function ( ){})( ) and (function ( ){}( )) and new Function() difference?

new Function()

There is still a difference,
fn = new Function("arg1","alert(arg1)"); This is to create a new function,
Function is a class, similar to the prototype of Array, String, Number, Object, etc.

such as var arr=new Array ();

and all functions inherit the properties of Function~~
Function is a constructor, which is used to dynamically build a function.
function is a keyword, and the function generated by function is actually a Function object instance

Execute the function immediately:

You need to understand the principle of IIFE, simply say:

function foo() {...} // This is the definition, Declaration; the definition just lets the interpreter know it exists, but it doesn't run. 
foo();                    // This is a statement, Statement; the interpreter will run it when it encounters a statement.

IIFE is not required, traditionally it can be written like this

So why IIFE?
1. The traditional method is verbose, and the definition and execution are written separately;
2. The traditional method directly pollutes the global namespace (the global object in the browser, such as window)

Therefore, developers want to find a way of writing that can solve the above problems. So is it okay to write like the following?

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

 

Of course not, but why? Because the function foo(...){} part is just a declaration, to the interpreter it's as if you wrote a string "function foo(...){}", which requires the use of a parsing function such as eval () to execute it. So putting () directly after the declaration will not execute, which is wrong syntax.

How to get it right? It's easy to say, just turn the declaration into an Expression.

In fact, there are still many ways to transform expressions. The most common way is to wrap the function declaration with a pair of (), so it becomes:

( function foo() {...})     // This is a deliberate newline, which can actually be combined with the following parentheses 
();

This is equivalent to:

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

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

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

So you ask is there a difference? Very simple: wood has ~

In addition, I just said that there are many ways to change expressions, and there are indeed many other ways to write them, such as:

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

or

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

Any of these will work.

You can also cast expressions with void, since this keyword does not return a value. But it really doesn't matter

void  function () {
     // here is the real code 
}();

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

void  function (global) {
     // here, global is the global object 
}( this )     // in the browser, this is the window object

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325022244&siteId=291194637