Daniel Matthew

Two forms of IIFE

TIL there are two ways of creating an IIFE - and there's heated discussion between parties as to why they have chosen their preferred style. Douglas Crockford prefers the second structure here, as the first "looks like dog balls". If you use JSLint, you'll be encouraged to adopt the latter style - it suggests the parentheses that invoke the function should sit within those that contain it.

(function(){
  function foo() {
    // I'm not polluting the global scope!
  }
})();
(function(){
  function bar() {
    // Nor am I!
  }
}());

Daniel is a front-end developer at Talis, with an interest in accessibility, performance, and web components.