catch()为参数创建块作用域

2023-11-20  本文已影响0人  Time_Notes

try中捕获到错误以后,会把异常对象推入一个可变对象并置于作用域的头部。在catch代码块内部,函数的所有局部变量将会被放在第二个作用域对象中,catch中的代码执行完,会立即销毁当前作用域。


try{

    console.log(a) //undefined

    throw new Error("err")

}catch(e){

    var a ="a" //非参数变量提升到外部

    console.log(e)//"err"

}

console.log(a) //"a"

console.log(e) //ReferenceError catch为参数创建一个块作用域,外部无法引用


var e = 'e'

try{

    throw new Error("err")

}catch(e){ //将异常传递给catch函数作为参数

    console.log(e) //Error

}

console.log(e) //e


e = 'e'

try{

    throw new Error("err")

}catch(e){

    var b='b'; //catch里的非参数声明提升到了外部

    console.log(e) // Error

}

console.log(e) //'e'

console.log(b) //'b'


e = 'e'

try{

    throw new Error("err")

}catch(e){

    console.log(e) //Error

    e ="in catch"

    console.log(e) //"in catch"

}

console.log(e) //'e'

catch内部的e已经更改了,但外部的e还是原始值,这说明catch的确创建了一个作用域,catch作用域的内部变量e的值并没有影响到外部作用域的e的值。


e = 'e'

try{

    a = 'aa'

    e = 'ee'

    throw new Error("err")

}catch(e){

    console.log(a) //'aa'

    var a='aaa';

    console.log(a)//'aaa'

    console.log(e)//Error

    e ="in catch"

    console.log(e)//'in catch'

}

console.log(e)//'ee'

console.log(a)//'aaa'

catch创建的块作用域,只对catch的参数有效。

对于在内部声明的变量,catch并没有创建一个新的作用域,只是一个普通的代码块。


// This is a JavaScript Quiz from BFE.dev

The try...catch statement marks a try block and a catch block. If the code in the try block throws an exception then the code in the catch block will be executed. In all these examples, we are throwing an exception in try which is why the control immediately goes to the catch block.

The important thing to know is that the catch-block specifies an identifier that holds the value of the exception; this value is only available in the scope of the catch-block

var a = 'a'

try {

  throw new Error('BFE.dev')

} catch { // no local variable being used

  var a = 'a1' // overwrites outer varibale a, redeclaring global a

}

console.log(a) // a1

var b = 'b'

try {

  throw new Error('BFE.dev')

} catch (b) { // local variable b references the passed error

  var b = 'b1' // No longer pointing to the global variable, its a locally scoped variable only

}

console.log(b) //'b'

var c = 'c'

try {

  throw new Error('BFE.dev')

} catch (error) { // local variable error references the passed error

  var c = 'c1' // overwrites outer variable c, redeclaring global c

}

console.log(c) // c1

We have just written catch and are not even capturing the error parameter so var a declared inside actually becomes global and overwrites outer a hence printing a1 later

We have written catch(b) which means we are using b to hold the exception value which is only available locally inside this catch block. Hence, even after declaring b inside, the global value remains unchanged. Thus, printing b

We have written catch(error) and are using error variable to capture the error value , so error is a locally scoped variable but c is not. Hence, similar to 1. var c declared inside actually becomes global and overwrites outer c hence printing c1 later.

上一篇下一篇

猜你喜欢

热点阅读