为什么不const和let语句在window对象上定义 [英] why don't const and let statements get defined on the window object

查看:48
本文介绍了为什么不const和let语句在window对象上定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们以以下代码为例:

Let's take the following code for example:

const constVar = 'some string';
let letVar = 'some string';
var varVar = 'some string';

(function() {
  console.log(window.constVar); // prints undefined
  console.log(window.letVar); // prints undefined
  console.log(window.varVar); // prints 'some string'
})();

根据mdn对const语句的描述:

According to the description of the const statement by mdn:

此声明创建一个常量,其范围对于声明它的块可以是全局的或局部的.

This declaration creates a constant whose scope can be either global or local to the block in which it is declared.

我认为let的工作方式相同.

And I assume let works in the same way.

在这种情况下,块"包含在全局范围内.我想这里的重要区别是,尽管const constVar是全局"可访问的,但仍然没有在window对象上对其进行定义.

In this case, the "block" is contained within the global scope. I guess the important distinction here is that while const constVar is "globally" accessible it still doesn't define it on the window object.

这使我认为全局范围和window对象是完全不同的.最终导致2个问题.

Which leads me to think that global scope and the window object are disparate. Which ultimately leads to 2 questions.

  1. 为什么在window上定义了使用var关键字声明的变量,而在window上未定义使用constlet声明的变量?

  1. Why do variables declared using the var keyword get defined on window and variables declared with const and let not defined on window?

全局范围"和浏览器提供给我们的window对象之间有什么区别.

What is the difference between "global scope" and the window object provided to us by browsers.

推荐答案

1.为什么在窗口上定义使用var关键字声明的变量,而在窗口上未定义使用constlet声明的变量?

1. Why do variables declared using the var keyword get defined on window and variables declared with const and let not defined on window?

因为规范是这样说的.如果您要问这个决定背后的原因,那么您应该与规范维护者联系.

Because the specification says so. If you are asking for the reason behind that decision, then you should reach out to the specification maintainers.

无论如何,类也不会成为全局对象的属性.

Classes do not become properties of the global object either btw.

2. 全局范围"和浏览器提供给我们的window对象之间有什么区别.

2. What is the difference between "global scope" and the window object provided to us by browsers.

有两种类型的基本环境记录根据规范:

There are two types of base environment records according to the spec:

  • 声明性环境记录
  • 对象环境记录

声明性环境记录基本上是您在调用函数时获得的标准环境.所有绑定(变量,常量等)都是在某些内部数据结构中定义的,而这些结构无法从普通代码中访问.

A Declarative Environment Record is basically your standard environment that you get when calling a function. All bindings (variables, constants, etc) are defined in some internal data structure that cannot be accessed from normal code.

另一方面,对象环境记录使用实际的JavaScript对象存储绑定.例如,现在已不推荐使用的with语句使用了此命令:

An Object Environment Record on the other hand uses an actual JavaScript object to store bindings. This is used for example by the now deprecated with statement:

with({foo: 42}) {
  console.log(foo);
}

现在, 全局环境记录 实际上包含两个环境记录:声明性环境记录和对象环境记录.对象环境由全局对象(即window)支持,并包含var声明和浏览器提供的其他全局变量.声明性环境包含letconstclass等声明.

Now, a Global Environment Record actually consists of two environment records: A declarative environment record and an object environment record. The object environment is backed by the global object, i.e. window and contains var declarations and other globals that the browser provides. The declarative environment contains the let, const, class, etc declarations.

这篇关于为什么不const和let语句在window对象上定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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