美文网首页
catch()为参数创建块作用域

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

作者: Time_Notes | 来源:发表于2023-11-20 07:11 被阅读0次

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.

相关文章

  • 你不知道的 Javascript

    作用域 词法作用域:编译阶段确定(欺骗词法作用域 eval with) 块作用域 with try/catch l...

  • 你不知道的JS-上卷

    作用域整体理解:JS作用域分为函数作用域,全局作用域,with和try catch形成的块级作用域。 JS引擎在编...

  • JS学习系列 03 - 函数作用域和块作用域

    在 ES5 及之前版本,JavaScript 只拥有函数作用域,没有块作用域(with 和 try...catch...

  • JavaScript笔记一

    早在ES3中JavaScript就引入了块级作用域, 如: with catch二个块级作用域, 只是没有专门定义...

  • 作用域

    作用域 javascript没有块级作用域 只有函数可以创建的作用域

  • JavaScript作用域探幽(二)

    函数作用域和块作用域 Javascript有基于函数的作用域,而在ES6的版本以前,除了少数机制能创建块作用域之外...

  • 2.17

    作用域 一共有三种全局作用域函数作用域 let const 的块级作用域 ! 函数调用 .call第一个参数...

  • ES6学习笔记之块级作用域

    ES5 只有全局作用域和函数作用域,没有块级作用域,let实际上为 JavaScript 新增了块级作用域。 例子...

  • ES5与ES6部分对比

    常量声明对比 作用域对比 块作用域对比 箭头函数 箭头函数会改变this的指向 默认参数 可变参数 就是在不确定传...

  • 2018-01-15

    延长作用域链~~with、eval、try-catch

网友评论

      本文标题:catch()为参数创建块作用域

      本文链接:https://www.haomeiwen.com/subject/bxdmrktx.html