TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Semicolons matter

77 点作者 josep2大约 9 年前

30 条评论

bilalq大约 9 年前
It still surprises me that this discussion pops up so often. Even more surprising is that people assume as a matter of fact that forgetting to put a semi-colon is the problem.<p>&gt; When your code’s intention is ambiguous, your JavaScript engine will attempt to fill in the gaps.<p>This is kind of misleading. The JS engine isn&#x27;t making some guess. There are clearly defined semantics on how ASI (automatic semi-colon insertion) operates. Let&#x27;s put aside whether ASI is good or bad-it&#x27;s just part of JavaScript. Ending all your lines with semi-colons won&#x27;t save you, only understanding ASI will do that.<p>Take this example:<p><pre><code> function foo() { return &#x27;A really long string that I&#x27;ve put on another line&#x27;; } console.log(foo()); </code></pre> You&#x27;ll see that `foo` returned undefined here. Why? Because ASI terminated `return\n` as a statement.<p>Isaac Schlueter wrote a post on this years ago that pretty much says everything I&#x27;d want to say on this topic: <a href="http:&#x2F;&#x2F;blog.izs.me&#x2F;post&#x2F;2353458699&#x2F;an-open-letter-to-javascript-leaders-regarding" rel="nofollow">http:&#x2F;&#x2F;blog.izs.me&#x2F;post&#x2F;2353458699&#x2F;an-open-letter-to-javascr...</a>
评论 #11482436 未加载
评论 #11493835 未加载
评论 #11482074 未加载
评论 #11481607 未加载
ArmandGrillet大约 9 年前
Non-ambiguous titles also matter, in this case it should be &quot;Semicolons matter in JavaScript&quot;. Too many links have this problem and it makes it difficult to find an article when searching for something on HN.
评论 #11480819 未加载
jack9大约 9 年前
His example has at least 2 missing semicolons.<p>function Logger() { }<p>&#x2F;&#x2F; Log something to the console at a specified level<p>Logger.prototype.log = function(level) {<p><pre><code> console[level || &#x27;log&#x27;].apply(console,[].slice.apply(arguments, 1)); </code></pre> }; &#x2F;&#x2F; here<p>&#x2F;&#x2F; Sugar functions for Logger.log<p>[&#x27;info&#x27;, &#x27;warn&#x27;, &#x27;error&#x27;].forEach(function(level) {<p><pre><code> Logger.prototype[level] = function () { return this.log.apply(this, [level].concat(Array.from(arguments))); }; &#x2F;&#x2F; here </code></pre> });
评论 #11485719 未加载
评论 #11480966 未加载
评论 #11481037 未加载
epmatsw大约 9 年前
Seems like everyone should just be using an automated tool to catch missing&#x2F;extra semicolons. No reason not to at this point, and eslint even autofixes them for you.
评论 #11481441 未加载
评论 #11480436 未加载
bantunes大约 9 年前
&quot;Error establishing a database connection&quot; - caching matters.
评论 #11480796 未加载
评论 #11480889 未加载
IshKebab大约 9 年前
Kind of insane that this still hasn&#x27;t been fixed. You should be able to put something like `strict_semicolons;` at the start of a JS file to tell the interpreter to <i>require</i> semicolons for that file.
msie大约 9 年前
Flame-on: I wish I could blacklist other people&#x27;s code that don&#x27;t use semicolons. Perhaps that flagging should be put into a linter. That linter will then update a global list of semicolon-optional code. Flame-off.
k__大约 9 年前
Semicolons cannot save you: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Qlr-FGbhKaI" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Qlr-FGbhKaI</a><p>Gist: Adding semicolons won&#x27;t help, because you still have to know how JavaScript inserts them automatically to add them in the right places.
评论 #11481501 未加载
bluedino大约 9 年前
&gt;&gt; Be unambiguous with your code. If you’re ending a statement, use a semicolon.<p>We&#x27;re still going over this? <i>Javascript, The Good Parts</i> came out close to 10 years ago and went over this.
评论 #11481036 未加载
评论 #11480808 未加载
adrusi大约 9 年前
Honestly when you already have to keep track of JavaScript&#x27;s other gotchas, having to remember to look for likes that start with ( or [ and precede then with a semicolon is not that much to ask. It seems silly anyway, but not particularly damaging.
58028641大约 9 年前
I never used semicolons and I have never had a problem. Just avoid ambiguous code.
评论 #11480459 未加载
aerovistae大约 9 年前
Can someone explain what&#x27;s happening here? I&#x27;m a JavaScript dev, perfectly familiar with JS and with semi-colon insertion, but I&#x27;m not following the code after &quot;This is how V8 sees it:&quot;<p>I don&#x27;t see how or why the two lengthy statements using `apply` are being omitted entirely. I also don&#x27;t see how that&#x27;s &quot;perfectly valid code&quot; . . . I tried running it and got an error &#x27;cannot read property forEach of undefined.&#x27;
评论 #11481187 未加载
评论 #11481521 未加载
评论 #11483507 未加载
encoderer大约 9 年前
Isn&#x27;t it as simple as: You only need a semi colon if the next line starts with &quot;[&quot; or &quot;(&quot; ?
评论 #11481342 未加载
评论 #11481284 未加载
degif大约 9 年前
Google cache version - <a href="http:&#x2F;&#x2F;goo.gl&#x2F;x6jLVW" rel="nofollow">http:&#x2F;&#x2F;goo.gl&#x2F;x6jLVW</a>
Animats大约 9 年前
The trend towards optional semicolons isn&#x27;t good. Javascript, Go, and Rust all have different semicolon syntax and semantics.
评论 #11482658 未加载
评论 #11481338 未加载
评论 #11481363 未加载
z3t4大约 9 年前
Whether you are writing semicolons or not. Don&#x27;t start a new line with (, [, &#x2F;, +, ++, or --. And never have a line break after return, break or continue.
lacker大约 9 年前
I would prefer finding a subset of JavaScript in which omitting semicolons is okay. Maybe this could be enforced in a linter. For example, if you never begin statements with [ or (, would it be okay to omit all semicolons?<p>A semicolon doesn&#x27;t seem like much but that&#x27;s a lot of extra typing over the course of a career.
评论 #11482644 未加载
评论 #11482298 未加载
pbreit大约 9 年前
I don&#x27;t mind putting them in (not really true) but am still not always certain where they go.
评论 #11480433 未加载
erikpukinskis大约 9 年前
Meh. I don&#x27;t use semicolons at all in JavaScript, except in for loops. I&#x27;ve never once had a bug from it.<p>I&#x27;ll stick to caring about he myriad issues that actually cause quality problems for me rather than worry about obscure theoretical bugs.
Wintamute大约 9 年前
Are Semicolons Necessary in JavaScript? <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=gsfbh17Ax9I" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=gsfbh17Ax9I</a>
gregmac大约 9 年前
Obligatory: <a href="https:&#x2F;&#x2F;pbs.twimg.com&#x2F;media&#x2F;B5LU4F7CUAELcIU.png" rel="nofollow">https:&#x2F;&#x2F;pbs.twimg.com&#x2F;media&#x2F;B5LU4F7CUAELcIU.png</a>
iLemming大约 9 年前
OMG. Reasonable people already learned that Clojurescript, Elm and Purescript are much, much, much better, so why are you still debating about semicolons?
draw_down大约 9 年前
My team doesn&#x27;t use them (except in a for-loop), hasn&#x27;t for years, and it&#x27;s never actually been a problem. Folks need to let this one go, imo.
评论 #11480918 未加载
swang大约 9 年前
I will write the function,<p>While you go have a beer<p>Where is my module support?<p>Where is my linter library?<p>Where is my code ending?<p>Where have all the semicolons gone?<p>--- Sing to Paula Cole&#x27;s &quot;Where have all the Cowboys Gone?&quot;
amelius大约 9 年前
When I click on the link, I get &quot;Error establishing a database connection&quot;. Probably a semicolon missing somewhere.
alexandrerond大约 9 年前
Why is this discussion always attached to JavaScript and not to Ruby or Go?
soyuka大约 9 年前
they don&#x27;t matter unless necessary
spinningarrow大约 9 年前
Knowing what you&#x27;re doing matters.
musha68k大约 9 年前
Feelings matter
proksoup大约 9 年前
I like working on code bases without semi colons better than those with, because the choice to drop is indicative of the team&#x27;s attitudes, and I often find many other things I like about those softwares.<p>The rules to drop them are simpler than the rules to keep them.
评论 #11481390 未加载