💡 자바스크립트 코드가 실행되기 위한 정보들을 가진 공간(객체)을 의미하는 추상적 개념
기본 실행 컨텍스트로, 함수 내부에 없는 코드는 이 전역 컨텍스트에서 실행된다.
window
전역 객체를 생성한다this
를 전역 객체로 설정한다함수가 호출될 때마다 해당 함수에 대한 새로운 컨텍스트가 생성된다.
eval
함수 내에서 생성되는 실행 컨텍스트이다
실행 가능한 코드를 실행하기 위한 정보들을 모아둔 곳
환경 레코드 (Environment Record)
: 유효 범위(Scope) 안에 있는 식별자와 결과값이 Key Pair로 저장하는 공간
호이스팅
이 가능해지는 원리bindObject
프로퍼티에 저장해 사용한다with
문외부 렉시컬 환경 참조 (Outer Lexical Environment Reference)
: 해당 함수를 둘러싸고 있는 외부 영역에 대한 참조를 연결리스트로 관리하는 곳
[[scopes]]
프로퍼티에 해당하는 정보💡 식별자에 대한 유효범위를 뜻한다.
전역 스코프와 지역 스코프로 구분되며, 전역 스코프에 선언된 변수는 어디에서든 접근 가능한 반면, 지역 스코프는 선언된 함수의 내부에서만 가능하다.
스코프체인이란 식별자의 유효범위를 찾아 지역 스코프에서 외부 렉시컬 환경 참조를 이용해 상위 스코프를 탐색해 나가는 과정을 말한다.
💡 렉시컬 환경 컴포넌트
- 변경 사항이 실시간으로 반영된다
- 따라서 일반적으로 함수의 로컬 스코프 범위를 뜻한다
- 함수 선언과 변수 바인딩(
let
,const
)을 저장💡 변수 환경 컴포넌트
- 선언 시점의 렉시컬 환경 컴포넌트 정보 (식별자 정보, 외부 환경 정보)의 스냅샷
- 변경 사항을 반영하지 않는다
- 변수
var
로 선언된 변수를 저장
this
값의 할당을 결정한다.
window
)let a = 20;
const b = 30;
var c;
function multiply(e, f){
var g = 20;
return e * f * g;
}
c = multiply(20, 30);
렉시컬 환경 컴포넌트와 변수 환경 컴포넌트를 생성한다.
let
, const
var
GlobalExecutionContext = {
LexicalEnvironment: {
EnvironmentRecord: {
Type: "Object",
//Identifier bindings go here
a: < uninitialized >, // 1. 선언 완료 // 초기화는 X
b: < uninitialized >, // 1. 선언 완료 // 초기화는 X
multiply: < func >
}
outer: <null>
ThisBinding: <Global Object>
},
VariableEnvironment: {
EnvironmentRecord: {
Type: "Object",
// Identifier bindings go here
c: undefined, // 1. 선언 & 초기화 동시에 진행 완료
}
outer: <null>
ThisBinding: <Global object>
}
}
모든 변수에 대한 값 할당을 완료하고 코드를 실행한다
GlobalExecutionContext = {
LexicalEnvironment: {
EnvironmentRecord: {
Type: "Object",
//Identifier bindings go here
a: 20,
b: 30,
multiply: < func >
}
outer: <null>
ThisBinding: <Global Object>
},
VariableEnvironment: {
EnvironmentRecord: {
Type: "Object",
// Identifier bindings go here
c: undefined,
}
outer: <null>
ThisBinding: <Global object>
}
}
FUnctionExecutionContext = {
LexicalEnvironment: {
EnvironmentRecord: {
Type: "Declarative",
//Identifier bindings go here
Arguments: {0: 20, 1: 30, length: 2},
},
outer: <GlobalLexicalEnvironment>,
ThisBinding: <Global Object or undefined>,
},
VariableEnvironment: {
EnvironmentRecord: {
Type: "Declarative",
//Identifier bindings go here
g: undefined
},
outer: <GlobalLexicalEnvironment>,
ThisBinding: <Global Object or undefined>
}
}
FUnctionExecutionContext = {
LexicalEnvironment: {
EnvironmentRecord: {
Type: "Declarative",
//Identifier bindings go here
Arguments: {0: 20, 1: 30, length: 2},
},
outer: <GlobalLexicalEnvironment>,
ThisBinding: <Global Object or undefined>,
},
VariableEnvironment: {
EnvironmentRecord: {
Type: "Declarative",
//Identifier bindings go here
g: 20
},
outer: <GlobalLexicalEnvironment>,
ThisBinding: <Global Object or undefined>
}
}
https://curryyou.tistory.com/237
https://jsyovo.tistory.com/40?category=942594
https://samslow.github.io/development/2020/07/06/JIT/
https://velog.io/@namezin/javascript-동작-원리
https://blog.toycrane.xyz/진짜-쉽게-알아보는-자바스크립트-동작-원리-c7fbdc44cc97