Vue: 为什么代码里都不写分号呢?

Created on 10 May 2016  ·  5Comments  ·  Source: vuejs/vue

Vue.js version

1.0.16

Reproduction Link

Steps to reproduce

What is Expected?

What is actually happening?

Most helpful comment

See: https://github.com/yyx990803/semi

Why???

Because style.

On a more serious note, some people (including me) feel more comfortable and productive when writing JavaScript without semicolons. (If you disagree, read this) However, this is often not viable in a team environment, or when working on a project with established code style requirements.

And: https://www.zhihu.com/question/20298345/answer/49551142

没有应该不应该,只有你自己喜欢不喜欢。JavaScript 语法长得 C-like 不代表它本质上和 C 是一类语言,所有直觉性的 “当然应该加分号” 都是保守的、未经深入思考的草率结论。后来新设计的语言里可选分号的多得去了,光是 “可以加分号但是大家都不加” 的语言就有:Go, Scala, Ruby, Python, Swift, Groovy...

至于说 “很难总结什么时候加不加”,其实真的很简单。真正会导致上下行解析出问题的 token 有 5 个:括号,方括号,正则开头的斜杠,加号,减号。我还从没见过实际代码中用正则、加号、减号作为行首的情况,所以总结下来就是一句话:一行开头是括号或者方括号的时候加上分号就可以了,其他时候全部不需要。其实即使是这两种情况,在实际代码中也颇为少见。

另外,restricted production 这个东西(也就是导致 return 后面换行会自动插入分号的机制),不管你加不加分号你都是得搞懂了才能不被坑的,和加不加分号没有什么关系。

All 5 comments

本来我也习惯写分号的,但是用尤大提供的vue-cli时写分号会报错,后来就不写分号,然后慢慢觉得分号很丑了,然后回去写java时也时不时忘记写分号

代码风格的问题,不觉得ES6+去分号,代码非常清爽么

See: https://github.com/yyx990803/semi

Why???

Because style.

On a more serious note, some people (including me) feel more comfortable and productive when writing JavaScript without semicolons. (If you disagree, read this) However, this is often not viable in a team environment, or when working on a project with established code style requirements.

And: https://www.zhihu.com/question/20298345/answer/49551142

没有应该不应该,只有你自己喜欢不喜欢。JavaScript 语法长得 C-like 不代表它本质上和 C 是一类语言,所有直觉性的 “当然应该加分号” 都是保守的、未经深入思考的草率结论。后来新设计的语言里可选分号的多得去了,光是 “可以加分号但是大家都不加” 的语言就有:Go, Scala, Ruby, Python, Swift, Groovy...

至于说 “很难总结什么时候加不加”,其实真的很简单。真正会导致上下行解析出问题的 token 有 5 个:括号,方括号,正则开头的斜杠,加号,减号。我还从没见过实际代码中用正则、加号、减号作为行首的情况,所以总结下来就是一句话:一行开头是括号或者方括号的时候加上分号就可以了,其他时候全部不需要。其实即使是这两种情况,在实际代码中也颇为少见。

另外,restricted production 这个东西(也就是导致 return 后面换行会自动插入分号的机制),不管你加不加分号你都是得搞懂了才能不被坑的,和加不加分号没有什么关系。

What @fnlctrl said, also this type of question doesn't belong here. Please read the contributing guide.

有道理~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

julianxhokaxhiu picture julianxhokaxhiu  ·  3Comments

bdedardel picture bdedardel  ·  3Comments

fergaldoyle picture fergaldoyle  ·  3Comments

6pm picture 6pm  ·  3Comments

loki0609 picture loki0609  ·  3Comments