持续吐槽 Js……

7 阅读1分钟

众所周知,PHP 是最好的语言。

而 JS 则是最魔幻的语言,持续记录使用中的魔幻时刻:

  1. 学了一半的in
!(1 in {1: 1})  // false
1 not in {1: 1}  # false
  1. break就会“漏”的switch
const foo = 0;
switch (foo) {
  case -1:
    console.log("negative 1");
    break;
  case 0: // Value of foo matches this criteria; execution starts from here
    console.log(0);
  // Forgotten break! Execution falls through
  case 1: // no break statement in 'case 0:' so this case will run as well
    console.log(1);
    break; // Break encountered; will not continue into 'case 2:'
  case 2:
    console.log(2);
    break;
  default:
    console.log("default");
}
// Logs 0 and 1
foo = 0
match foo:
    case -1:
        print("negative 1")
    case 0:
        print(0)
    case 1:
        print(1)
    case 2:
        print(2)
    case _:
        print("default")