javascript
javascript 分号_让我们谈谈JavaScript中的分号
javascript 分號
要使用它們,還是不使用它們… (To use them, or not to use them…)
Semicolons in JavaScript divide the community. Some prefer to use them always, no matter what. Others like to avoid them.
JavaScript中的分號分隔社區。 有些人更喜歡始終使用它們,無論如何。 其他人喜歡避免它們。
I put out a poll on Twitter to test the waters, and I found lots of semicolon supporters:
我在Twitter上進行了一項民意測驗以測試水域,然后發現了許多分號支持者:
After using semicolons for years, in the fall of 2017 I decided to try avoiding them when I could. I set up Prettier to automatically remove semicolons from my code, unless there was a particular code construct that required them.
在使用分號多年之后,在2017年秋天,我決定盡量避免使用分號。 我將Prettier設置為自動從代碼中刪除分號,除非存在需要它們的特殊代碼構造。
Now I find it natural to avoid semicolons, and I think the code looks better and is cleaner to read.
現在,我發現避免分號是很自然的事情,并且我認為代碼看起來更好,更易于閱讀。
This is all possible because JavaScript does not strictly require semicolons. When there is a place where a semicolon is needed, it adds it behind the scenes.
這都是可能的,因為JavaScript并不嚴格要求分號。 當有需要分號的地方時,它將其添加到幕后。
This is called Automatic Semicolon Insertion.
這稱為自動分號插入 。
It’s important to know the rules that power semicolons. This will allow you to avoid writing code that will generate bugs before it does not behave like you expect.
了解支持分號的規則很重要。 這將使您避免編寫在行為不如預期的情況下會生成錯誤的代碼。
JavaScript自動分號插入規則 (The rules of JavaScript Automatic Semicolon Insertion)
The JavaScript parser will automatically add a semicolon when, during the parsing of the source code, it finds these particular situations:
JavaScript解析器在解析源代碼期間發現以下特殊情況時,將自動添加分號:
when the next line starts with a }, closing the current block
當下一行以}開頭時,關閉當前塊
when there is a return statement on its own line
當在自己的行上有一個return語句時
when there is a break statement on its own line
當一行上有一個break語句時
when there is a throw statement on its own line
當在自己的行上有一個throw語句時
when there is a continue statement on its own line
當在自己的行上有一個continue語句時
不符合您的想法的代碼示例 (Examples of code that does not do what you think)
Based on those rules, here are some examples.
根據這些規則,下面是一些示例。
Take this:
拿著這個:
const hey = 'hey'const you = 'hey'const heyYou = hey + ' ' + you['h', 'e', 'y'].forEach((letter) => console.log(letter))You’ll get the error Uncaught TypeError: Cannot read property 'forEach' of undefined because based on rule 1, JavaScript tries to interpret the code as
您將收到錯誤Uncaught TypeError: Cannot read property 'forEach' of undefined因為基于規則1 ,JavaScript嘗試將代碼解釋為
const hey = 'hey';const you = 'hey';const heyYou = hey + ' ' + you['h', 'e', 'y'].forEach((letter) => console.log(letter))This piece of code:
這段代碼:
(1 + 2).toString()prints "3".
打印"3" 。
const a = 1const b = 2const c = a + b(a + b).toString()Instead, it raises a TypeError: b is not a function exception, because JavaScript tries to interpret it as
相反,它引發TypeError: b is not a function異常,因為JavaScript嘗試將其解釋為
const a = 1 const b = 2 const c = a + b(a + b).toString()Another example based on rule 4:
基于規則4的另一個示例:
(() => { return { color: 'white' }})()You’d expect the return value of this immediately-invoked function to be an object that contains the color property, but it’s not. Instead, it’s undefined, because JavaScript inserts a semicolon after return.
您希望此立即調用的函數的返回值是一個包含color屬性的對象,但事實并非如此。 相反,它是undefined ,因為JavaScript在return之后插入一個分號。
Instead you should put the opening bracket right after return:
相反,您應該在return后將左括號放在右邊:
(() => { return { color: 'white' }})()You’d think this code shows ‘0’ in an alert:
您可能會認為此代碼在警報中顯示“ 0”:
1 + 1 -1 + 1 === 0 ? alert(0) : alert(2)but it shows 2 instead, because JavaScript (per rule 1) interprets it as:
但它改為顯示2,因為JavaScript(根據規則1)將其解釋為:
1 + 1 -1 + 1 === 0 ? alert(0) : alert(2)結論 (Conclusion)
Be careful — some people are very opinionated about semicolons. I don’t care, honestly. The tool gives us the option not to use it, so we can avoid semicolons if we want.
小心-有些人對分號很自以為是。 老實說,我不在乎。 該工具為我們提供了不使用它的選項,因此我們可以避免使用分號。
I’m not suggesting anything on one side or the other. Just make your own decision based on what works for you.
我不建議任何方面。 只需根據適合您的條件做出自己的決定。
Regardless, we just need to pay a bit of attention, even if most of the time those basic scenarios never show up in your code.
無論如何,即使在大多數情況下,這些基本方案在您的代碼中始終沒有出現,我們只需要稍微注意一下即可。
Pick some rules:
選擇一些規則:
Be careful with return statements. If you return something, add it on the same line as the return (same for break, throw, continue)
注意return語句。 如果您返回內容,則將其添加到返回內容的同一行(與break , throw , continue )
- Never start a line with parentheses, as those might be concatenated with the previous line to form a function call, or an array element reference 切勿以括號開頭,因為括號可能會與前一行連接在一起以形成函數調用或數組元素引用
 
And ultimately, always test your code to make sure it does what you want.
最后,請始終測試您的代碼以確保它能夠滿足您的要求。
I publish 1 free programming tutorial per day on flaviocopes.com, check it out!
我每天在flaviocopes.com上發布1個免費的編程教程,請查看!
Originally published at flaviocopes.com.
最初發布于flaviocopes.com 。
翻譯自: https://www.freecodecamp.org/news/lets-talk-about-semicolons-in-javascript-f1fe08ab4e53/
javascript 分號
總結
以上是生活随笔為你收集整理的javascript 分号_让我们谈谈JavaScript中的分号的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 梦到把蛇关起来预示着什么
 - 下一篇: 如何确定Ionic是否适合您的项目