4 ms·
There are some caveats. This, for example, would not work as expected: foo() [1,2,3].forEach(bar) Automatic semicolon insertion wouldn't work here, becaus
by jannes 15y ago
There are some caveats. This, for example, would not work as expected:
foo()
[1,2,3].forEach(bar)
Automatic semicolon insertion wouldn't work here, because the bracket could be a continuation of the last line. The same goes for parentheses at the beginning of a line.
Source: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding http://blog.izs.me/post/2353458699/an-open-letter-to-javascr...
- einaregilsson 15y agoYes, the two cases where I actually think I could run into that are that and var x = i (function() {...})() where it wil think i( is a function call.
- sjs 15y agoThe informal and single easy rule is to prefix lines that begin with punctuation with a semi. In practice only [ and ( crop up, almost nobody begins lines with a prefix increment or decrement. If you use += 1 and -= 1 instead you sidestep that case.
- felipemnoa 15y agoOr, you could just add a semicolon. KISS at work.
- sjs 15y agoThat's perfectly fine as long as you're honest about why you do it. Most people here are misinformed.
- einaregilsson 15y agoYes, you could just add a semicolon in front of lines that start with punctuation (tiny percentage of all lines) or you could just stick them at the end of every single line...
- eropple 15y agoYes, you could put them at the end of every statement. Because consistency is a good thing.
- einaregilsson 15y ago@eropple: So you put semicolons after every line? Even after block statements {} ? Why not, if the goal is to be consistent?
- burgerbrain 15y agoYou should use the reply button.
- einaregilsson 15y agoIt wasn't visible for me on that one comment. I don't know why, I thought it was too deply nested or something, but now I see it. Weird.
- sjs 15y agoThe Reply link doesn't show up for a minute or two. I guess it's supposed to force you to think instead of firing off a hasty response. Kind of pointless for one liners but hey...