Types of Execution Context
Global Execution Context
Functional Execution Context
Eval Function Execution Context
처음 코드를 실행(브라우저가 스크립트를 로딩하여 실행하는 것)하는 순간 전역 컨텍스트(global execution context)가 생성되고, execution stack(콜스택)의 가장 아래 위치하게 된다.
→ 모든 실행 컨텍스느의 가장 베이스에서는 Global Execution Context가 존재!
선입선출
Execution context in ES5ExecutionContext = { VariableEnvironment : { ... }, LexicalEnvironment : { ... }, ThisBinding: <this value> }
This Binding
전역 실행 컨텍스트에서, this의 값은 전역 객체를 참조한다.
(브라우저일 시, 윈도우 객체 참조)
함수 실행 컨텍스트에서, this의 값은 함수가 어떻게 호출되는지에 달렸다. 만약 객체 참조로부터 호출되면, this의 값은 해당 객체로 set 되고, 그렇지않을 경우, 전역 객체 또는 undefined(strict 모드)로 set 된다.
const person = {
name: 'peter',
birthYear: 1994,
calcAge: function() {
console.log(2018 - this.birthYear);
}
}
person.calcAge();
// 'this' 는 'person'을 참조한다.
//왜냐하면, 'calcAge'는 'person' 객체 참조로 호출됐기 때문이다.
const calculateAge = person.calcAge;
calculateAge();
// 'this' 는 전역 윈도우 객체를 참조한다.
// 왜냐하면, 아무럼 객체 참조도 주어지지 않았기 때문이다.
Key Point
Execution context 2가지 단계를 가지고 있다.
var x = 10;
function foo(){
var y = 20;
console.log(x+y); // 30
}
// Environment는 기술적으로 2가지 메인 구성요소가 존재한다:
// environmentRecord, and a reference to the outer environment
// global context의 Environment
globalEnvironment = {
environmentRecord: {
// built-ins
// our bindings:
x: 10
},
outer: null // no parent environment
};
// "foo" function의 Environment
fooEnvironment = {
environmentRecord: {
y: 20
},
outer: globalEnvironment
};
변수, arguments, 함수선언문을 초기저장하기 위해 쓰인다.
(나중에 context가 활동하는 단계로 들어갈때 채워진다.) → 말들이 다 다르다. 추후 더 공부하고 수정 계획
function foo(a) {
var b = 20;
}
foo(10);
// creation stage때의 foo함수의 VariableEnvironment 구성요소
fooContext.VariableEnvironment = {
environmentRecord: {
arguments: { 0: 10, length: 1, callee: foo },
a: 10,
b: undefined
},
outer: globalEnvironment
};
// execution stage이후, VE envRec가 값으로 채워진다.
fooContext.VariableEnvironment = {
environmentRecord: {
arguments: { 0: 10, length: 1, callee: foo },
a: 10,
b: 20
},
outer: globalEnvironment
};
초기에, VariableEnvironment를 복사한다.
context가 실행될 때, context에서 나타내는 식별자의 바인딩을 결정하는데 쓰인다.
ES6에서, 렉시컬 환경 컴포넌트와 변수 환경 컴포넌트의 차이점은, 전자는 함수 선언과 변수(let and constance) 바인딩을 저장하는 데 사용되며, 후자는 변수(var) 바인딩만 저장하는 데 사용된다.
참조 :