5 ms·
Not having to use semicolons sounds nice, but in practice it can have terrible effects. E.g.: var x = function() { // something } // avoid polluti
by Nitramp 15y ago
Not having to use semicolons sounds nice, but in practice it can have terrible effects. E.g.:
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.
- Calvein 15y agoYou can write this without any problem: var x = function() { // something } // avoid polluting global scope: !function() { // Some initialization. }()
- evan_ 15y agoSo now you've gone from a semicolon, which has the sole purpose of separating statements; to a bang, which doesn't make any sense in this context unless you know that it's replacing a semicolon. Better or worse?
- insin 15y agoYou could use anything which separates statements or ensures the function is treated as an expression. Two examples which are more in keeping with the intended use of the operators in question: ;function() { // Some initialization. }() void function() { // Some initialization. }()
- mkopinsky 15y agoSo now we've gone a) from semicolon consistently after statements, to having to remember where to prefix with semicolons, or b) from one character to four. Why aren't we just using the semicolon as it is required in any other (C-based) language?
- mistercow 15y agoJS's syntax is influenced by C via its pretensions of being somehow related to Java, but I think it's a stretch to call it a "C-based language".
- ricardobeat 15y agoFirst commandment of semi-colon-free: any line that starts with [,(,+,- should be prefixed with a semi-colon. 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.
- Michiel 15y agoGood advice, and interesting because languages and syntax are interesting to me. But I would never introduce this in my team as a replacement for semicolons at the end of each statement. It seems too random a rule to just follow, hence you'd need to understand the specific problem that it solves. At that point your mental model has become slightly more complex, which doesn't seem worth it.
- ricardobeat 15y agoIt's absolutely not random. You only put semi-colons where needed. There is no optional use. '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.
- mistercow 15y agoBut if that really is the only rule needed, then that should be very easy to add to a linter.