JavaScript catch子句范围 [英] JavaScript catch clause scope

查看:49
本文介绍了JavaScript catch子句范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ECMAScript 5规范指出以下内容:

通常,词汇环境与某些特定的语言相关联ECMAScript代码的语法结构,例如FunctionDeclaration,WithStatement或TryStatement的Catch子句以及新的Lexical每次评估此类代码时都会创建环境.

Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a WithStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated.

如果我的理解是正确的,则在JavaScript中创建新的词法环境时,将输入新的作用域,这就是为什么在函数内部声明的变量在该函数外部不可见的原因:

If my understanding is correct, then when a new Lexical Environment is created in JavaScript, a new scope is entered, which is why variables declared inside a function are not visible outside of that function:

function example() {
    var x = 10;
    console.log(x); //10
}
console.log(x); //ReferenceError

因此,在以上函数声明中,创建了一个新的词法环境,这意味着 x 在可能存在的任何外部词法环境中均不可用.

So in the above function declaration, a new Lexical Environment is created, which means x is not available in any outer Lexical Environments that may exist.

因此,上面引用的有关函数声明的部分似乎很有意义.但是,它也指出为Try语句的Catch子句创建了一个新的词法环境:

So the part of the quote above about Function Declarations seems to make sense. However, it also states that a new Lexical Environment is created for the Catch clause of a Try Statement:

try {
    console.log(y); //ReferenceError so we enter catch
} 
catch(e) {
    var x = 10;
    console.log(x); //10
}
console.log(x); //10 - but why is x in scope?

那么 catch 块的作用范围如何?我对词法环境是什么有基本的误解?

So how does the scope of a catch block work? Do I have a fundamental misunderstanding of what a Lexical Environment is?

推荐答案

如果我理解正确,那么这可能意味着在您的代码中,

If I understand it right, then what it probably means is that, in your code,

try {
    console.log(y); //ReferenceError so we enter catch
} 
catch(e) {
    var x = 10;
    console.log(x); //10
}

e 仅存在于catch块中.在catch块外尝试 console.log(e); ,它将抛出ReferenceError.

e will only exist in the catch block. Try console.log(e); outside the catch block and it will throw ReferenceError.

与WithStatement一样, with({x:1,y:2}){} ,x和y仅存在于with块内.

As with WithStatement, with ({x: 1, y: 2}) { }, x and y will only exist inside the with block.

但这并不意味着 var 声明将绑定到最接近的词法环境.实际上,在进入执行上下文时, var 声明将绑定到环境.

But it doesn't mean that var declarations will be bound to the closest lexical environment. Actually, var declarations will be bound to the environment when entering an execution context.

10.5声明绑定实例化:在输入执行上下文时,它将查找函数声明,参数,和变量声明,然后在执行上下文的VariableEnvironment中创建绑定.

10.5 Declaration Binding Instantiation: when the execution context in being entered, it will look for function declarations, arguments, and variable declarations and then create bindings in the execution context's VariableEnvironment.

因此,使用 var 声明的任何变量都可以在函数中的任何位置访问,而不管控件的结构或在函数内部的定义位置.请注意,这不包括嵌套函数,因为它们是单独的执行上下文.

So any variables that are declared using var will be accessible anywhere in the function regardless of the control structures or where it is defined inside the function. Note that this does not include nested functions as they're a separate execution context.

这意味着 var 声明将绑定到最接近的执行上下文.

That means the var declarations will be bound to the closest execution context.

var x = 1;
(function() {
    x = 5; console.log(x); // 5
    if (false) { var x; }
    x = 9; console.log(x); // 9
})();
console.log(x); // 1

因此在上面的代码中, x = 5; 将在内部函数中设置 x 变量,因为 var x; if(false){var x;} 在执行功能代码之前已经绑定到该功能.

So in above code, x = 5; will set the x variable inside the inner function, because var x; inside if (false) { var x; } is bound to that function already before the function code is executed.

这篇关于JavaScript catch子句范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆