我在用 JavaScript 做一个测试,我注意到使用 let
关键字就像
let variable = "I am a variable";
console.log(window.variable);
做了not add a variable
property to the window
object .这是否意味着它在全局范围内不存在?
我可以声明变量而不必在函数或 IIFE 中限定它们的范围吗?
我确实搜索过这个,但找不到任何东西。每个人都说要使用像 {}()
这样的函数,或者只是访问一个包含代码的唯一全局变量,但看到这个让我想知道它是否可以用于避免这些事情的目的。
最佳答案
不能使用 let
语句以您建议的方式避免使用全局变量。顶级 let
语句仍会在全局范围内创建变量,只是不会将它们创建为“全局对象”(通常是 window
)的属性。
<script>
'use strict';
let fromLet = 'from let';
var fromVar = 'from var';
</script>
<script>
console.log(fromLet); // 'from let'
console.log(fromVar); // 'from var'
console.log(window.fromLet); // undefined
console.log(window.fromVar); // 'from var'
</script>
此行为主要在 Section 8.1.1.4: Global Environment Records of the ECMAScript 6 specification 中描述.它的要点是有一个单一的全局范围/命名空间,但它的值可以记录在两个不同的地方。全局内置函数、function
声明、var
声明和function*
(生成器)声明被记录为全局对象的属性,而所有内容else global(来自 let
、const
、class
和其他新构造)存储在内部“环境记录”中,不会暴露为运行时中的一个对象。
如果您想深入了解细节,可以从以下一些相关摘录开始。
A global Environment Record is used to represent the outer most scope that is shared by all of the ECMAScript Script elements that are processed in a common Realm (8.2). A global Environment Record provides the bindings for built-in globals (clause 18), properties of the global object, and for all top-level declarations (13.2.8, 13.2.10) that occur within a Script.
Properties may be created directly on a global object. Hence, the object Environment Record component of a global Environment Record may contain both bindings created explicitly by FunctionDeclaration, GeneratorDeclaration, or VariableDeclaration declarations and binding created implicitly as properties of the global object. In order to identify which bindings were explicitly created using declarations, a global Environment Record maintains a list of the names bound using its CreateGlobalVarBindings and CreateGlobalFunctionBindings concrete methods.
Table 18 — Additional Fields of Global Environment Records
[[ObjectRecord]]
: Object Environment RecordBinding object is the global object. It contains global built-in bindings as well as FunctionDeclaration, GeneratorDeclaration, and VariableDeclaration bindings in global code for the associated Realm.
[[DeclarativeRecord]]
: Declarative Environment RecordContains bindings for all declarations in global code for the associated Realm code except for FunctionDeclaration, GeneratorDeclaration, and VariableDeclaration bindings.
[[VarNames]]
: List of StringThe string names bound by FunctionDeclaration, GeneratorDeclaration, and VariableDeclaration declarations in global code for the associated Realm.
关于javascript - 可以在 JavaScript 中使用 "let"关键字来避免全局范围的变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36343969/