在 JavaScript 中声明和初始化变量之间的区别是什么?
ECMAScript 规范中对变量的声明和初始化进行了如下陈述 −
A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. [...] A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.
以上定义了区别
- 所有变量都使用 undefined 值初始化。
- 在初始化词法环境时对变量声明使用 undefined 值进行初始化。
- 这个初始化不能当作一个赋值。
广告