Help with Javascript !function() declaration

I’ve been looking all over for an explanation of this function declaration but cant find it. Can someone answer what it means to have a function with no name, just declared as this. When does it execute? How does it get called?

!function() {
/* all the code in here omitted*/
} ();

For reference, here’s the function:

!function ()
var b = document.documentElement, c = “DOMControl_placeholder”;
var a = function (d)
var g;
var d = d || window.event, e = || d.srcElement, f = e.getAttribute(“placeholder”);
if (f)
if (“focus” == d.type || “focusin” == d.type)
if (e.value == f)
e.value = “”;
CSS.removeClass(e, c);
if (e.value == “”)
CSS.addClass(e, c);
e.value = f;
if (e.value == “” || e.value == f)
g = e.parentNode;
CSS.addClass(g, “xyz”);
CSS.removeClass(g, “xyz”);
b.onfocusin = b.onfocusout = a;
if (b.addEventListener)
b.addEventListener(“focus”, a, true);
b.addEventListener(“blur”, a, true);

That function is called once it finishes loading - that’s what the last () on the end means. Without the final () it would never be run as anonymous functions cannot be called any other way unless they are assigned to something with a name that can be used to reference them.

Basically using an anonymous function in that way keeps everything it uses out of the global namespace.

Not sure why the ! on the front as all that does is invert the result that the function returns (false instead of true or true instead of false) but since there is nothing in the code to use the returned value that shouldn’t make any difference.

Okay, just realised what the ! is for.

A statement that starts with the word “function” is considered to be a statement having the format

function foo() {...}

The function deing defined there is trying to use tha alternate format

var foo = function() {...}

but without assigning the function to a variable. By using the ! it indicates that this code is using this second variant of a function definition instead of the first. The more conventional way to do this is to wrap the entire function in parentheses.

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

the ! version is one character shorter but adds extra processing to invert the result.

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