Airbnd suggests I do this:
!function() {
// ...
}();
Because:
This ensures that if a malformed module forgets to include a final semicolon there aren't errors in production when the scripts get concatenated.
The bang allows me to work around the language's grammar rules:
// Evaluated in Chromium 34 console.
function(){}(); // => SyntaxError: Unexpected token (
!function(){}(); // => true
And when concatenating other modules the bang seems to do the trick:
!function(){}();function(){}(); // => SyntaxError: Unexpected token (
!function(){}();!function(){}(); // => true
(function(){}());!function(){}(); // => true
However it doesn't seem to actually be "safe," because if someone else doesn't have a semi-colon at the end of his script:
!function(){}()!function(){}(); // => SyntaxError: Unexpected token !
(function(){}())!function(){}(); // => SyntaxError: Unexpected token !
It would appear that a leading semi-colon IIFE is better.
;(function() {
// ...
}());
!function(){}();(function(){}()); // => undefined
(function(){}());(function(){}()); // => undefined
!function(){}();;(function(){}()); // => undefined
(function(){}());;(function(){}()); // => undefined
Am I missing something? Is it actually acceptable to use bang "!" functions or are leading semi-colon ";" IIFEs truly superior because of the way they concatenate?
!
in front of a function declaration to make it get evaluated as a function expression... Also what the heck does "a leading semi colon is the dealbreaker" mean? – Egwin