如何让你的JS代码更好看易读(2)
链式调用写法
// bad
fetch(url).then(res => {
return res.json()
}).then(() => {
// doSomething
}).catch(e => {
})
// good
fetch(url)
.then(res => {
return res.json()
})
.then(() => {
// doSomething
})
.catch(e => {
})
保持代码是纵向发展的
发现那些在整个文件中特别'突出'的代码时,应该考虑对他们做换行处理了
// bad return handleClick(type, key, ref, self, source, props) // good return handleClick( type, key, ref, self, source, props ) // bad const a = this.props.prop_a === 'hello' ? <di>world</div> : null // good const a = this.props.prop_a === 'hello' ? <di>world</div> : null
