逗号

  • 19.1 行首逗号:不需要

    1. // bad
    2. const story = [
    3. once
    4. , upon
    5. , aTime
    6. ];
    7. // good
    8. const story = [
    9. once,
    10. upon,
    11. aTime,
    12. ];
    13. // bad
    14. const hero = {
    15. firstName: 'Ada'
    16. , lastName: 'Lovelace'
    17. , birthYear: 1815
    18. , superPower: 'computers'
    19. };
    20. // good
    21. const hero = {
    22. firstName: 'Ada',
    23. lastName: 'Lovelace',
    24. birthYear: 1815,
    25. superPower: 'computers',
    26. };
  • 19.2 增加结尾的逗号: 需要

    为什么? 这会让 git diffs 更干净。另外,像 babel 这样的转译器会移除结尾多余的逗号,也就是说你不必担心老旧浏览器的尾逗号问题

    1. // bad - git diff without trailing comma
    2. const hero = {
    3. firstName: 'Florence',
    4. - lastName: 'Nightingale'
    5. + lastName: 'Nightingale',
    6. + inventorOf: ['coxcomb graph', 'modern nursing']
    7. }
    8. // good - git diff with trailing comma
    9. const hero = {
    10. firstName: 'Florence',
    11. lastName: 'Nightingale',
    12. + inventorOf: ['coxcomb chart', 'modern nursing'],
    13. }
    14. // bad
    15. const hero = {
    16. firstName: 'Dana',
    17. lastName: 'Scully'
    18. };
    19. const heroes = [
    20. 'Batman',
    21. 'Superman'
    22. ];
    23. // good
    24. const hero = {
    25. firstName: 'Dana',
    26. lastName: 'Scully',
    27. };
    28. const heroes = [
    29. 'Batman',
    30. 'Superman',
    31. ];