JavaScript压缩范围和大括号

时间:2022-08-26 22:34:31

As I understand the scope of const is curly braces (block). However, when I run the following code in browser (FF) it doesn't give any error. Is the following JS code right? Please, explain.

据我所知,const的范围是花括号(block)。但是,当我在浏览器(FF)中运行以下代码时,它不会产生任何错误。下面的JS代码对吗?请解释一下。

 <script type="text/javascript">
 {
     const Foo = "foo";        
 }
 </script>
 <div></div>
 <script type="text/javascript">
 {
     Foo = null;
 }
 </script>

2 个解决方案

#1


5  

You will not get error because,

你不会出错,因为,

Below code says you are using const variable of Foo inside this code block, the const Foo will bot be accessible out side of the block

下面的代码表示您正在这个代码块中使用const Foo, const Foo将在块之外访问

 {
     const Foo = "foo";        
 }

Below code says you are assigning the variable Foo with value of null, which will bind to window object,

下面的代码说,您正在为变量Foo赋值为null,该值将绑定到window对象,

 {
    Foo = null;
 }

so Foo = null; which binds to window object and const Foo='foo' is a constant variable for the particular code block

所以Foo =零;哪个绑定到窗口对象,const Foo=' Foo '是特定代码块的常量变量

So when you are assign Foo=null it assigns the variable to window object and you can access it like ,

当你赋值Foo=null时它将变量赋给window对象你可以像这样访问它,

  window.Foo

i.e. any variable which declare without var it will bind to window object,

也就是说,任何没有var声明的变量都会绑定到窗口对象,

#2


2  

You are aware that using const will create a constant in a given scope. Look at your example:

您知道使用const将在给定的范围内创建一个常量。看看你的例子:

<script type="text/javascript">
{
   const Foo = "foo";        
}
</script>
<div></div>
<script type="text/javascript">
{
   Foo = null;
}
</script>

You can reduce the above to the following code:

您可以将上述代码简化为以下代码:

{
   const Foo = "foo";        
}
{
   Foo = null;
}

Since you have declared and initialized Foo in a scope (denoted by using { and }), then Foo is only known to that scope. Let's call that scope A. When issuing Foo = null;, you are in a different scope: B. When assigning a value to a variable, the engine always checks if the variable name is not bound in the current execution context. In short, it checks

由于您已经在一个范围中声明并初始化了Foo(用{和}表示),那么Foo只对该范围是已知的。让我们调用作用域a。当发出Foo = null时,您处于不同的作用域:b。当为变量赋值时,引擎总是检查变量名是否在当前执行上下文中未被绑定。简而言之,它检查

  1. if Foo exists in the local scope: the answer is no. Your Foo = "foo" is only known to the scope A. Not in the current scope.
  2. 如果Foo存在于本地范围:答案是no。您的Foo = " Foo "仅为范围a所知,不在当前范围内。
  3. if Foo exists in the scope chain: the answer is no. There is no declarations made in the parent scope.
  4. 如果Foo存在于范围链中:答案是no。在父范围中没有作出声明。
  5. if Foo is known to the global execution context (aka window scope): the answer is no.
  6. 如果全局执行上下文(也称为窗口范围)知道Foo:答案是no。

End result: the assignment will occur. But since you did not have provided any assignment statements: const, let or var. Therefore, Foo=null will be added to the window scope. If you do window.Foo, you will get a response with null.

最终结果:分配将发生。但是由于您没有提供任何赋值语句:const、let或var。因此,Foo=null将被添加到窗口范围。如果你的窗口。Foo,你会得到一个带有null的响应。

However, it has to be noted that there should be an error. But Javascript is a language that "serves" the developer. Instead of spitting you with an error, it will silent that error and hook Foo to the window object. If you use strict mode, then it will throw an error. (please consult the MDN page for further information)

但是,必须指出,应该有一个错误。但是Javascript是一种“为”开发者服务的语言。它不会向您抛出错误,而是将使该错误静默,并将Foo挂接到窗口对象。如果您使用严格模式,那么它将抛出一个错误。(详情请参阅MDN网页)

To comprehend it a better, look to the following examples that is executed in a non-strict environment and think first if there would be an error or not.

要更好地理解它,请查看以下在非严格环境中执行的示例,并首先考虑是否会出现错误。

<script type="text/javascript">
{
     const Foo = 'hello';
     Foo = 555; // success or fail ?
}
</script>

The answer is no. You are aware of that. It fails the first check: Foo is already bound in the same scope.

答案是否定的。你知道的。第一次检查失败:Foo已经在相同的范围内绑定。

<script type="text/javascript">
{
     const Foo = 'hello';
     {
        Foo = 555; // success or fail ?
     }
}
</script>

The answer is yes. It will fail because it fails the second check (see the list above). Foo = 555; is called in a scope: B. Scope B has a parent scope: A where Foo is known.

答案是肯定的。它会失败,因为它失败了第二个检查(见上面的列表)。Foo = 555;在作用域中调用:B作用域B有父作用域:a, Foo是已知的。

Now, something tricky:

现在,一些技巧:

<script type="text/javascript">
    {
        window.Foo = 'hello';
        {
            Foo = 555; // success or fail ?
        }
    }
</script>

What do you think? ????

你怎么认为?????

...

it works. Do you not believe me: check it here below:

它的工作原理。你不相信我吗?请看下面:

{
  window.Foo = 'hello world';
  console.log('window.Foo?', window.Foo); // shows hello world
  {
    Foo = 555;
    console.log('window.Foo?', window.Foo); // shows 555
  }
}

that is because adding variables to the window scope (although it is not recommendable to do so) does not make it read-only. You can change it afterwards as desired.

这是因为向窗口范围添加变量(尽管不建议这样做)不会使它成为只读的。您可以根据需要更改它。

#1


5  

You will not get error because,

你不会出错,因为,

Below code says you are using const variable of Foo inside this code block, the const Foo will bot be accessible out side of the block

下面的代码表示您正在这个代码块中使用const Foo, const Foo将在块之外访问

 {
     const Foo = "foo";        
 }

Below code says you are assigning the variable Foo with value of null, which will bind to window object,

下面的代码说,您正在为变量Foo赋值为null,该值将绑定到window对象,

 {
    Foo = null;
 }

so Foo = null; which binds to window object and const Foo='foo' is a constant variable for the particular code block

所以Foo =零;哪个绑定到窗口对象,const Foo=' Foo '是特定代码块的常量变量

So when you are assign Foo=null it assigns the variable to window object and you can access it like ,

当你赋值Foo=null时它将变量赋给window对象你可以像这样访问它,

  window.Foo

i.e. any variable which declare without var it will bind to window object,

也就是说,任何没有var声明的变量都会绑定到窗口对象,

#2


2  

You are aware that using const will create a constant in a given scope. Look at your example:

您知道使用const将在给定的范围内创建一个常量。看看你的例子:

<script type="text/javascript">
{
   const Foo = "foo";        
}
</script>
<div></div>
<script type="text/javascript">
{
   Foo = null;
}
</script>

You can reduce the above to the following code:

您可以将上述代码简化为以下代码:

{
   const Foo = "foo";        
}
{
   Foo = null;
}

Since you have declared and initialized Foo in a scope (denoted by using { and }), then Foo is only known to that scope. Let's call that scope A. When issuing Foo = null;, you are in a different scope: B. When assigning a value to a variable, the engine always checks if the variable name is not bound in the current execution context. In short, it checks

由于您已经在一个范围中声明并初始化了Foo(用{和}表示),那么Foo只对该范围是已知的。让我们调用作用域a。当发出Foo = null时,您处于不同的作用域:b。当为变量赋值时,引擎总是检查变量名是否在当前执行上下文中未被绑定。简而言之,它检查

  1. if Foo exists in the local scope: the answer is no. Your Foo = "foo" is only known to the scope A. Not in the current scope.
  2. 如果Foo存在于本地范围:答案是no。您的Foo = " Foo "仅为范围a所知,不在当前范围内。
  3. if Foo exists in the scope chain: the answer is no. There is no declarations made in the parent scope.
  4. 如果Foo存在于范围链中:答案是no。在父范围中没有作出声明。
  5. if Foo is known to the global execution context (aka window scope): the answer is no.
  6. 如果全局执行上下文(也称为窗口范围)知道Foo:答案是no。

End result: the assignment will occur. But since you did not have provided any assignment statements: const, let or var. Therefore, Foo=null will be added to the window scope. If you do window.Foo, you will get a response with null.

最终结果:分配将发生。但是由于您没有提供任何赋值语句:const、let或var。因此,Foo=null将被添加到窗口范围。如果你的窗口。Foo,你会得到一个带有null的响应。

However, it has to be noted that there should be an error. But Javascript is a language that "serves" the developer. Instead of spitting you with an error, it will silent that error and hook Foo to the window object. If you use strict mode, then it will throw an error. (please consult the MDN page for further information)

但是,必须指出,应该有一个错误。但是Javascript是一种“为”开发者服务的语言。它不会向您抛出错误,而是将使该错误静默,并将Foo挂接到窗口对象。如果您使用严格模式,那么它将抛出一个错误。(详情请参阅MDN网页)

To comprehend it a better, look to the following examples that is executed in a non-strict environment and think first if there would be an error or not.

要更好地理解它,请查看以下在非严格环境中执行的示例,并首先考虑是否会出现错误。

<script type="text/javascript">
{
     const Foo = 'hello';
     Foo = 555; // success or fail ?
}
</script>

The answer is no. You are aware of that. It fails the first check: Foo is already bound in the same scope.

答案是否定的。你知道的。第一次检查失败:Foo已经在相同的范围内绑定。

<script type="text/javascript">
{
     const Foo = 'hello';
     {
        Foo = 555; // success or fail ?
     }
}
</script>

The answer is yes. It will fail because it fails the second check (see the list above). Foo = 555; is called in a scope: B. Scope B has a parent scope: A where Foo is known.

答案是肯定的。它会失败,因为它失败了第二个检查(见上面的列表)。Foo = 555;在作用域中调用:B作用域B有父作用域:a, Foo是已知的。

Now, something tricky:

现在,一些技巧:

<script type="text/javascript">
    {
        window.Foo = 'hello';
        {
            Foo = 555; // success or fail ?
        }
    }
</script>

What do you think? ????

你怎么认为?????

...

it works. Do you not believe me: check it here below:

它的工作原理。你不相信我吗?请看下面:

{
  window.Foo = 'hello world';
  console.log('window.Foo?', window.Foo); // shows hello world
  {
    Foo = 555;
    console.log('window.Foo?', window.Foo); // shows 555
  }
}

that is because adding variables to the window scope (although it is not recommendable to do so) does not make it read-only. You can change it afterwards as desired.

这是因为向窗口范围添加变量(尽管不建议这样做)不会使它成为只读的。您可以根据需要更改它。