by Nitramp · 14y ago · 0 comments
var x = function() {
// something
}
// avoid polluting global scope:
(function() {
// Some initialization.
})()
This will call the function x with the anonymous function as its argument, and then call the result of that.There are more examples to that point, and many of them really aren't all that straightforward. So don't rely on automatic semicolons.
var x = function() {
// something
}
// avoid polluting global scope:
;(function() {
// Some initialization.
})()
;[1,2,3].forEach(...)
In practice those are the only places you'll see (and need) them. It has the extra benefit of disencouraging starting a line with a pre-increment, hacks or weird constructs (not talking of IIFEs of course), and making sure you're never calling a function or accessing properties by accident.'semicolons at the end of each statement' is much more random. They are unnecessary after function declarations or conditionals, and you might end up forgetting them somewhere that actually matters, just because you're not paying attention. The mental effort for this change is severely overestimated.
var x = function() {
// something
}
// avoid polluting global scope:
!function() {
// Some initialization.
}()Better or worse?
;function() {
// Some initialization.
}()
void function() {
// Some initialization.
}()Why aren't we just using the semicolon as it is required in any other (C-based) language?