King丨小义 发表于 2022-9-29 20:36:10

这段JS代码的运行结果是?

(() => {
    let x, y;
    try{
      throw new Error();
    }catch(x){
      (x=1),(y=2);
      console.log(x);
    }
    console.log(x);
    console.log(y);
})()

ba21 发表于 2022-9-29 20:36:11

1
undefined
2

(() => {
    let x, y;
    try {

      throw new Error();

    } catch (z) { // 该处的x是个局部变量,为便于理解,我们可以改为z,所以z只在catch块有效。
      (x = 1), (z = 3), (y = 2);
      console.log(z);
    }
    console.log(x);
    console.log(y);
})()
页: [1]
查看完整版本: 这段JS代码的运行结果是?