Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Javascript

การประกาศและการเริ่มต้นตัวแปรใน 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.

ข้างต้นกำหนดความแตกต่าง:

  • ตัวแปรทั้งหมดเริ่มต้นด้วยค่าที่ไม่ได้กำหนดไว้
  • การประกาศตัวแปรจะเริ่มต้นโดยไม่มีการกำหนดเมื่อมีการเริ่มต้นของสภาพแวดล้อมคำศัพท์
  • การเริ่มต้นนี้ใช้ไม่ได้กับการมอบหมาย