JS

변수 - JS Deep Dive 와 ECMAScript 참고

myDav 2024. 5. 6. 03:09

변수를 사용하려면,

 

1. 변수 Declaration (선언)

  • 값을 저장하기 위한 메모리 공간 확보. 변수 이름과 메모리 공간의 주소 연결해서 값을 저장할 수 있도록 준비
  • var 키워드의 경우, 확보된 메모리 공간에는 undefined값이 암묵적으로 할당

2. 변수 Initialization (초기화)

  • 선언된 이후, 최초로 값을 할당하는 것
  • ECMA에서 살펴본 'let', 'const' 키워드 변수와 'var' 로 선언된 변수의 정의는 아래와 같다.
  • let, const 에 대한 설명
'let' and 'const' declarations define variables that are scoped to the running execution context's LexicalEnvironment. The variables are created when their containing Environment Record is instantiated but may not be accessed in any way until the variable's LexicalBinding is evaluated. A variable defined by a LexicalBinding with an Initializer is assigned the value of its Initializer's AssignmentExpression when the LexicalBinding is evaluated, not when the variable is created. If a LexicalBinding in a let declaration does not have an Initializer the variable is assigned the value undefined when the LexicalBinding is evaluated.


- let/const 는 실행중인 실행 컨텍스트의 LexicalEnvironment에 정의.
- let/const 선언은 Environment Record 가 인스턴스화 될 때 생성되지만, LexicalBinding이 평가될 때까지 사용할 수 없다.
- Initializer가 있으면, LexicalBinding이 평가될 때 (변수가 생성될때가 아니라), initializer의 값으로 할당된다. 
  (-> 변수가 생성될 때가 할당되는 게 아니라는 것은, var 에서 생성과 동시에 undefined 로 초기화 되는 걸 대비시키기 위해 써놓은 듯 하다.)
- Initializer가 없으면, LexicalBinding이 평가될 때, undefined로 할당된다.  
  • var 에 대한 설명
A var statement declares variables that are scoped to the running execution context's VariableEnvironment. Var variables are created when their containing Environment Record is instantiated and are initialized to undefined when created. Within the scope of any VariableEnvironment a common BindingIdentifier may appear in more than one VariableDeclaration but those declarations collectively define only one variable. 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.


- var 는 실행중인 실행 컨텍스트의 VariableEnvironment에 정의.
- var 는 Environment Record 가 인스턴스화 될 때 생성되고, 생성될 때 undefined 값으로 초기화된다.
- 공통 BindingIdentifier(변수 식별자)이 VariableEnvironment에 나타날 수 있지만, 하나의 단일 변수만을 정의한다.
  (-> var 키워드 없이 선언하고, 값을 변경했을 때(또는 재선언)의 상황을 말한 것 같다.)
- Initializer가 있으면, VariableDeclaration(변수 값 선언)이 실행될 때, initializer의 값으로 할당된다. (생성될 때가 아니라)

 

더보기

ECMA 13.3 장에서는 변수에 대해서 소개하는데, var를 먼저 소개하는 것이 아니라, es6에 도입된 let/const 에 대해 먼저 소개를 한다. 그만큼 var 보다는 let/const를 많이 권장하는 듯하다.

  • Hoisting 측면에서 차이점, 
    • var는 선언은 현재 스코프의 맨 위로 옮겨지고 (그리고 undefined로 설정되고), 초기화는 그자리에 남아 있는다.
    • let/const는 호이스팅 되지만 TDZ(Temporal Dead Zone) 이 적용되어, 선언 전에 액세스하려면 ReferenceError가 발생한다. 결국엔, 사용하기 전에 선언되어있어야 한다.
      • var, let, const, function, function*, class 키워드를 사용하는 식별자는 호이스팅되어 런타임 이전 단계에서 먼저 실행.

 


 

 

ECMAScript® 2023 Language Specification

The first and subsequent editions of ECMAScript have provided, for certain operators, implicit numeric conversions that could lose precision or truncate. These legacy implicit conversions are maintained for backward compatibility, but not provided for BigI

262.ecma-international.org