写一手漂亮的 JavaScript

介绍

看了很多best practice,却没有人教我怎么去写一手漂亮的js代码,今天我来讲讲我自己写js的经验

不要在代码中留大段注释掉的代码


  1. // bad

  2. // function add() {

  3. //   const a = b + c

  4. //   return a

  5. // }

  6. function add() {

  7.  return a + 1000

  8. }

  9. // good

  10. function add() {

  11.  return a + 1000

  12. }

适当地换行


  1. // bad

  2. function a() {

  3.  const {

  4.    state_a,

  5.    state_b,

  6.    state_c

  7.  } = this.state

  8.  this.setState({state_a: state_a * 2})

  9.  return 'done'

  10. }

  11. // good

  12. function a() {

  13.  const {

  14.    state_a,

  15.    state_b,

  16.    state_c

  17.  } = this.state

  18.  this.setState({state_a: state_a * 2})

  19.  return 'done'

  20. }

适当的添加注释,但不要疯狂的添加注释

  • 对一段代码或者一行特别需要注意的代码注释

  • 不要疯狂的注释,太啰嗦,漂亮的代码自己会说话


  1. // bad

  2. const a = 'a' // 这是a

  3. const b = 'b' // 这是b

  4. const c = 'c' // 这是c

  5. // good

  6. /**

  7. * 申明变量

  8. */

  9. const a = 'a'

  10. const b = 'b'

  11. const c = 'c'

将类似行为、命名的代码归类在一起


  1. // bad

  2. function handleClick(arr) {

  3.  const a = 1

  4.  arr.map(e => e + a)

  5.  const b = 2

  6.  return arr.length + b

  7. }

  8. // good

  9. function handleClick(arr) {

  10.  const a = 1

  11.  const b = 2

  12.  arr.map(e => e + a)

  13.  return arr.length + b

  14. }

在不破坏语义性的情况下,'能省则省'

  • 牢记js中函数是一等公民

  • 但是,如果省略到影响可读性了,就是失败的

  • 在可读性和简洁性至今必须选一个的话,永远先选可读性


  1. function add(a) {

  2.  return a + 1

  3. }

  4. function doSomething() {

  5. }

  6. // bad

  7. arr.map(a => {

  8.  return add(a)

  9. })

  10. setTimeout(() => {

  11.  doSomething()

  12. }, 1000)

  13. // good

  14. arr.map(add)

  15. setTimeout(doSomething, 1000)


  1. // bad

  2. const a = (v) => {

  3.  return v + 1

  4. }

  5. // good

  6. const a = v => v + 1

  7. // bad

  8. const b = (v, i) => {

  9.  return {

  10.    v,

  11.    i

  12.  }

  13. }

  14. // good

  15. const b = (v, i) => ({v, i})

  16. // bad

  17. const c = () => {

  18.  return (dispatch) => {

  19.    // doSomething

  20.  }

  21. }

  22. // good

  23. const c = () => dispatch => {

  24.  // doSomething

  25. }


  1. // bad

  2. const a = this.props.prop_a + this.props.prop_b

  3. this.props.fun()

  4. // good

  5. const {

  6.  prop_a,

  7.  prop_b,

  8.  fun

  9. } = this.props

  10. const a = prop_a + prop_b

  11. fun()

合理使用各种表达式


  1. // bad

  2. if (cb) {

  3.  cb()

  4. }

  5. // good

  6. cb && cb()

  7. // bad

  8. if (a) {

  9.  return b

  10. } else {

  11.  return c

  12. }

  13. // good

  14. return a ? b : c

  15. // bad

  16. if (a) {

  17.  c = a

  18. } else {

  19.  c = 'default'

  20. }

  21. // good

  22. c = a || 'default'

链式调用写法


  1. // bad

  2. fetch(url).then(res => {

  3.  return res.json()

  4. }).then(() => {

  5.  // doSomething

  6. }).catch(e => {

  7. })

  8. // good

  9. fetch(url)

  10.  .then(res => {

  11.    return res.json()

  12.  })

  13.  .then(() => {

  14.    // doSomething

  15.  })

  16.  .catch(e => {

  17.  })

保持代码是纵向发展的

  • 发现那些在整个文件中特别'突出'的代码时,应该考虑对他们做换行处理了


  1. // bad

  2. return handleClick(type, key, ref, self, source, props)

  3. // good

  4. return handleClick(

  5.  type,

  6.  key,

  7.  ref,

  8.  self,

  9.  source,

  10.  props

  11. )

  12. // bad

  13. const a = this.props.prop_a === 'hello' ? <di>world</div> : null

  14. // good

  15. const a = this.props.prop_a === 'hello'

  16.  ? <di>world</div>

  17.  : null

总结

个人经验,如有错误,还望指正


原文:https://segmentfault.com/a/1190000012244094


写一手漂亮的 JavaScript