在 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。
  • 此初始化不作为赋值使用。

更新于: 2020 年 6 月 13 日

436 次浏览

启动你的 职业

通过完成课程获得认证

开始
广告