[英]Use Let variable declaration with With Statement
I read about with statement from Ecma Specification and in there with statement evaluation like this.我从 Ecma Specification 中阅读了 with 声明,并在其中阅读了这样的声明评估。
If with statement use Lexical Environment for evaluation why my sample code not work?如果 with 语句使用词法环境进行评估,为什么我的示例代码不起作用?
const user = {
fullName: "Murad Sofiyev"
};
with(user) {
let firstName = "Tofiq";
fullName = "Tofiq Sofiyev";
}
console.log(user.firstName); // Undefined
console.log(user.fullName); // Tofiq Sofiyev
PS: I know with statement is deprecetaed but this is help me to understand Lexical Environment and Variable Environment:) PS:我知道 with 声明已被弃用,但这有助于我理解词法环境和变量环境:)
The {
following the with(user)
creates an entirely new LexicalEnvironment. with(user)
之后的{
创建一个全新的LexicalEnvironment。 There are actually two environments created there: the environment for the with
'd object, and the environment for the new {
block.实际上在那里创建了两个环境:
with
'd object 的环境和新{
块的环境。 When you use let firstName = "Tofiq";
当你使用
let firstName = "Tofiq";
, you're creating a new variable name inside the inner (block-scoped) environment. ,您正在内部(块范围)环境中创建一个新变量名。
This might be a bit more obvious if you can see that you can use with
without a paired {
:如果您可以看到可以在没有配对
{
的情况下使用with
,这可能会更明显一点:
const user = { firstName: 'foo', }; with(user); console.log('Not a syntax error');
Here, the environment created by the with
'd value is created, and goes completely unused.在这里,由
with
'd 值创建的环境被创建,并且完全未被使用。 (and there's no inner environment). (并且没有内部环境)。
Another issue is that firstName
doesn't exist on the object.另一个问题是 object 上不存在
firstName
。 If the object had the firstName
property and you used var
instead of let
, the new Tofiq
would be assigned to the object.如果 object 具有
firstName
属性并且您使用var
而不是let
,则新的Tofiq
将分配给 object。
const user = { firstName: 'foo', }; with(user) { var firstName = "Tofiq"; } console.log(user.firstName); // Tofiq // because var is not block-scoped // so `var firstName` finds an outer Lexical Environment with that same variable name
Using let
always creates a variable in block scope.使用
let
总是在块 scope 中创建一个变量。 So even though you use it inside the with
block, it is creating a new variable scope to the with
.因此,即使您在
with
块中使用它,它也会为with
创建一个新变量 scope 。 You might want that behaviour if you needed to do some calculations inside the block.如果您需要在块内进行一些计算,您可能需要这种行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.