Douglas Crockford says 'Dog Balls'
Why Stack Overflow recommends it
Stack Overflow users link it when explaining why the two IIFE parenthesis styles differ, citing Crockford's stated preference and the 'dog balls' remark.
Recommended by 0 people across 0 different months, 2012 to 2018.
What it is
A 1m37s clip of Douglas Crockford explaining JavaScript function invocation patterns, in which he says that the `(function(){})()` form of an immediately invoked function looks like 'dog balls' and prefers `(function(){}())`. It is a short excerpt from one of his talks, posted by the channel fourtones.
What people said
Cites Aadit M Shah · 2012-12 · Stack Overflow · on “JavaScript self-invoking functions”
There's no difference between them. Both are immediately invoked function expressions [LINK] . Some people like Douglas Crockford prefer the second method [this video] . Some prefer the first. Interestingly Crockford's JSLint [LINK] doesn't allow the first method, so I assume that the second one is more common. Oh, and if you use a function declaration instead of a function expression then you can call the function before it appears in the program. This is because declarations are hoisted in JavaScript: greet(); // This will work function greet() { alert("Merry Christmas!"); } The same is not true for function expressions: greet(); // This will not work var greet = function greet() { alert("Merry
Cites Bjorn · 2014-10 · Stack Overflow · on “Making private instance variable accessible to prototype methods enclosed in anonymous function”
Just a note, Douglas Crockford considers `})();` dogballs, and recommends `}());` instead when wrapping functions in parenthesis. [this video]
Cites Adam Rackis · 2014-12 · Stack Overflow · on “Self-executing anonymous function conventions”
The first two are the same, and differ by style only*; the last one is different in that it gives you the ability to control what the value of this will be inside the IIFE. For example (function(){ this.a = 12; }).call(foo); will add the property a to the object foo. *Of course Douglas Crockford has a preference [this video]
Cites Behrad Khodayar · 2017-02 · Stack Overflow · on “Location of parenthesis for auto-executing anonymous JavaScript functions?”
The difference just exist because Douglas Crockford doesn't like the first style for IIFEs [LINK] ! (seriuosly) As you can see in this video!! [this video] . The only reason for the existence of the extra wrapping () {in both styles} is to help make that section of code Function Expression , because Function Declaration cannot be immediately called. Some scripts / minify-ers just use + , ! , - & ~ instead of too parentheses. Like this: +function() { var foo = 'bar'; }(); !function() { var foo = 'bar'; }(); -function() { var foo = 'bar'; }(); ~function() { var foo = 'bar'; }(); And all these are exactly the same as your alternatives. Choosing among these cases is completely on your own & makes no
Cites Patrick Roberts · 2018-02 · Stack Overflow · on “Can someone please explain Douglas Crockford's uber method?”
Maybe if he didn't use one letter variable names for everything... I'd have to study the rest of the linked page for a while to be able to explain everything here clearly. Douglas has had some pretty bizarre opinions on JavaScript "best practices" and this is one of those cases I think highlights just that point. Another notable case is where he claims that the end of `(function () {...})()` looks like "dog balls" [this video] , and that was his reason for preferring `(function () {...}())`