加载中...

19. 逗号


  • 19.1 行首逗号:不需要

    // bad
    const story = [
        once
      , upon
      , aTime
    ];
    
    // good
    const story = [
      once,
      upon,
      aTime,
    ];
    
    // bad
    const hero = {
        firstName: 'Ada'
      , lastName: 'Lovelace'
      , birthYear: 1815
      , superPower: 'computers'
    };
    
    // good
    const hero = {
      firstName: 'Ada',
      lastName: 'Lovelace',
      birthYear: 1815,
      superPower: 'computers',
    };
  • 19.2 增加结尾的逗号: 需要

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

      // bad - git diff without trailing comma
      const hero = {
           firstName: 'Florence',
      -    lastName: 'Nightingale'
      +    lastName: 'Nightingale',
      +    inventorOf: ['coxcomb graph', 'modern nursing']
      }
    
      // good - git diff with trailing comma
      const hero = {
           firstName: 'Florence',
           lastName: 'Nightingale',
      +    inventorOf: ['coxcomb chart', 'modern nursing'],
      }
    
      // bad
      const hero = {
        firstName: 'Dana',
        lastName: 'Scully'
      };
    
      const heroes = [
        'Batman',
        'Superman'
      ];
    
      // good
      const hero = {
        firstName: 'Dana',
        lastName: 'Scully',
      };
    
      const heroes = [
        'Batman',
        'Superman',
      ];

还没有评论.