目录
1.代码规则
JavaScript Standard Style
https://github.com/standard/standard/blob/master/docs/README-zhcn.md
- 使用两个空格 – 进行缩进
- 字符串使用单引号 – 需要转义的地方除外
- 不再有冗余的变量 – 这是导致 大量 bug 的源头!
- 无分号
- 行首不要以
(
,[
, or`
开头- 这是省略分号时唯一会造成问题的地方 – 工具里已加了自动检测!
- 关键字后加空格
if (condition) { ... }
- 函数名后加空格
function name (arg) { ... }
- 坚持使用全等
===
摒弃==
一但在需要检查null || undefined
时可以使用obj == null
。 - 一定要处理 Node.js 中错误回调传递进来的
err
参数
2.代码无分号问题
当一行是以( [ ` 开头时,在前面补上一个分号用以避免一些语法解析错误
` 是ES6新增的一种字符串包裹方式,叫做:模板字符串
它支持换行
function say() {
console.log('hello world')
}
say()
;(function () {
console.log('hello')
})()
;['苹果', '香蕉'].forEach(function (item) {
console.log(item)
})
var foo = `
言念其人
温其如玉
`
console.log(foo)
;`hello`.toString()