在 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 值進行初始化。
  • 這個初始化不能當作一個賦值。

更新日期: 13-Jun-2020

436 次瀏覽

開啟你的 職業生涯

完成課程後獲得認證

開始學習
廣告
© . All rights reserved.