What is the (function() { } )() construct in JavaScript?

I used to know what this meant, but I'm struggling now...

Is this basically saying document.onload ?

(function () {

})();

It's an Immediately-Invoked Function Expression, or IIFE for short. It executes immediately after it's created.

It has nothing to do with any event-handler for any events (such as document.onload ).
The first pair of parentheses (function(){...}) turns the code within (in this case, a function) into an expression, and the second pair of parentheses (function(){...})() calls the function that results from that evaluated expression.

This pattern is often used when trying to avoid polluting the global namespace, because all the variables used inside the IIFE (like in any other normal function) are not visible outside its scope.
This is why, maybe, you confused this construction with an event-handler for window.onload , because it's often used as this:

(function(){
    // all your code here
    var foo = function() {};
    window.onload = foo;
    // ...
})();
// foo is unreachable here (it’s undefined)

Correction suggested by Guffa:

The function is executed right after it's created, not after it is parsed. The entire script block is parsed before any code in it is executed. Also, parsing code doesn't automatically mean that it's executed, if for example the IIFE is inside a function then it won't be executed until the function is called.


It's just an anonymous function that is executed right after it's created.

It's just as if you assigned it to a variable, and used it right after, only without the variable:

var f = function () {
};
f();

In jQuery there is a similar construct that you might be thinking of:

$(function(){
});

That is the short form of binding the ready event:

$(document).ready(function(){
});

An immediately-invoked function expression (IIFE) immediately calls a function. This simply means that the function is executed immediately after the completion of the definition.

Three more common wordings:

// Crockford's preference - parens on the inside
(function() {
  console.log('Welcome to the Internet. Please follow me.');
}());

//The OPs example, parentheses on the outside
(function() {
  console.log('Welcome to the Internet. Please follow me.');
})();

//Using the exclamation mark operator
//https://stackoverflow.com/a/5654929/1175496
!function() {
  console.log('Welcome to the Internet. Please follow me.');
}();

If there are no special requirements for its return value, then we can write:

!function(){}();  // => true
~function(){}(); // => -1
+function(){}(); // => NaN
-function(){}();  // => NaN

Alternatively, it can be:

~(function(){})();
void function(){}();
true && function(){ /* code */ }();
15.0, function(){ /* code */ }();

You can even write:

new function(){ /* code */ }
31.new function(){ /* code */ }() //If no parameters, the last () is not required
链接地址: http://www.djcxy.com/p/51676.html

上一篇: 将eval()限制在一个狭窄的范围内

下一篇: JavaScript中的(function(){})()构造是什么?