3 ms·
You're right that your (very contrived) examples are different. But if anybody on my team writes code like this, it doesn't pass code review.
by wldlyinaccurate 11y ago
You're right that your (very contrived) examples are different. But if anybody on my team writes code like this, it doesn't pass code review.
- bshimmin 11y agoSure, those examples are quite contrived. Here's one that's not: console.log("woof") (function() { console.log("foo") })() Without semi-colons, that blows up (with a super unhelpful error message, to boot). Sure, if you understand ASI in JS, then you'll figure out the problem very quickly. But if you just always use semi-colons, then ASI is one fewer thing to have to worry about.
- insin 11y agoAlternatively: console.log("woof") void function() { console.log("foo") }() There really is only one ASI rule you need to remember [0] in practice: don't begin a new line with ( or [ if it's intended to be a new statement. On this topic, it could so easily have been different; I found this interesting from Brendan Eich [1]: > I wish I had made newlines more significant in JS back in those ten days in May, 1995. Then instead of ASI, we would be cursing the need to use infix operators at the ends of continued lines, or perhaps or brute-force parentheses, to force continuation onto a successive line. But that ship sailed almost 17 years ago. > ...My two cents: be careful not to use ASI as if it gave JS significant newlines... [0] this is technically a lie because everybody needs learn the other one whether they use semicolons or not: don't put a linebreak after the "return" keyword :) [1] https://brendaneich.com/2012/04/the-infernal-semicolon/ https://brendaneich.com/2012/04/the-infernal-semicolon/
- Bahamut 11y agovoid blows up on Safari.
- benihana 11y ago>There really is only one ASI rule you need to remember [0] in practice: don't begin a new line with ( or [ if it's intended to be a new statement. Here's an even easier and clearer one: finish every statement with a semicolon.
- insin 11y agoThis is precisely why I hate optional syntax, regardless of which way your preferences fall on this particular one.
- swang 11y agofunction hello() { var x = 0; return { x: x, y: y }; }; Just putting semicolons where you think it can work won't help you. If you write javascript, you must know what the asi does. So semicolons or no semicolons is completely irrelevant.
- bakabaka9 11y agoMindlessly inserting unnecessary syntax instead of learning the damn language. Hacker News should be proud of its lusers.
- taco_emoji 11y agoI don't have a photographic memory. If "learning the language" means rote memorization of an inconsistent set of rules for how the fucking code is parsed, then you're goddamn right I'd rather mindlessly insert a character that obviates that.