简体   繁体   中英

Is the switch example usage in airbnb javascript guide inconsistent?

I'm catching up on javascript that I have not used since some time, doing so by inspiring most of my coding style from the Modern Javascript coding guide by AirBnb

In the paragraph regarding switch I think there is an inconsistency. But that's not as if the Airbnb guide had been read by tons of guy before me...

Check the line for case 4 which alone does not use curly braces, but rather no braces at all like in the style before which is considered bad. However the default case comes back at using curly braces.

switch (foo) {
  case 1: {
    let x = 1;
    break;
  }
  case 2: {
    const y = 2;
    break;
  }
  case 3: {
    function f() {
      // ...
    }
    break;
  }
  case 4:
    bar();
    break;
  default: {
    class C {}
  }
}

If it is not an inconsistency, then what would be the sound reason for this different case 4 ?

There is no inconsistency. The curly braces are used as a scope for the lexical declarations. Case 4 is the only case without any such declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM