What does "!function () {}" mean/do in javascript?

Possible Duplicate:
What does the exclamation mark do before the function?
! preceding function in javascript?
javascript function leading bang ! syntax

I've been seeing this pattern a little bit recently in javascript:

!function () {
    // do something
}()

what does the bang in front of the function keyword supposed to do? I can't seem to find anything about it on the intertubez.


function () {
    // do something
}();

This is an immediately invoked function declaration. A function declaration can not be immediately invoked; it is a syntax error.

To get around this syntax error, most people enclose the IIFD in parens to force it to be an expression instead (IIFE).

(function () {
    // do something
})();

In this case, they added an exclamation point instead.

链接地址: http://www.djcxy.com/p/96888.html

上一篇: JavaScript中的自动执行功能的目的是什么?

下一篇: “!function(){}”是什么意思/在JavaScript中做?