19. 逗號

2018-02-24 16:11 更新
  • 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?增加結(jié)尾的逗號:?需要。

    為什么? 這會讓 git diffs 更干凈。另外,像 babel 這樣的轉(zhuǎn)譯器會移除結(jié)尾多余的逗號,也就是說你不必擔心老舊瀏覽器的尾逗號問題。

      // 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',
      ];
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號