5. I will note that there are some real
improvements in ES5, in particular to
Chapter 10 which now uses declarative
binding environments. ES1-3's abuse of
objects for scopes (again I'm to blame
for doing so in JS in 1995, economizing on objects
needed to implement the language in a big hurry) was
a bug, not a feature.
https://mail.mozilla.org/pipermail/es-discuss/2010-April/010915.html
13. Components of Lexical Environment
lexical environment = {
environment record : -,
outer environment reference : -
}
지역 식별자 덩어리
유효범위 내의 식별자를 바인딩
중첩 유효범위를 가질 수 있는 환경에
서 상위 lexical environment를 참조
14. 2 type of environment record
Declarative
vs
Object
19. function sum(x, y){
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
Global
Execution Context
sum’s
Execution Context
Function Code
20. thisBinding
sunFunctionEC.thisBinding = null;
function sum(x, y){
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
{
ThisBinding : null,
}
sumFunctionEC
21. thisBinding
sunFunctionEC.thisBinding = null;
function sum(x, y){
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
{
ThisBinding : null,
}
sumFunctionEC
ThisBinding이 null, undefined인 경우
this = global
“use strict” 모드에서는
this = null
32. outer environment reference
The outer environment reference is used
to model the logical nesting of Lexical Environment values
33. var a = 10;
(function foo() {
var b = 20;
(function bar() {
var c = 30;
console.log( a + b + c );
})();
})();
Identifier Resolution #1
Global
Execution Context
foo’s
Execution Context
bar’s
Execution Context
34. var a = 10;
(function foo() {
var b = 20;
(function bar() {
var c = 30;
console.log( a + b + c );
})();
})();
LexicalEnvironment : {
EnvironmentRecord : {
c : 30
}
OuterEnvironmentReference :
fooEC.LexicalEnvironment
},
bar’s EC
LexicalEnvironment : {
EnvironmentRecord : {
b : 20
}
OuterEnvironmentReference :
globalEC.LexicalEnvironment
},
foo’s EC
LexicalEnvironment : {
EnvironmentRecord : {
a : 10
}
OuterEnvironmentReference : null
},
Global EC
Identifier Resolution #1
35. var a = 10;
(function foo() {
var b = 20;
(function bar() {
var c = 30;
console.log( a + b + c );
})();
})();
LexicalEnvironment : {
EnvironmentRecord : {
c : 30
}
OuterEnvironmentReference :
fooEC.LexicalEnvironment
},
barEC
LexicalEnvironment : {
EnvironmentRecord : {
b : 20
}
OuterEnvironmentReference :
globalEC.LexicalEnvironment
},
fooEC
LexicalEnvironment : {
EnvironmentRecord : {
a : 10
}
OuterEnvironmentReference : null
},
globalEC
Identifier Resolution #2
37. A lexical environment that defines the environment
in which a Function object is executed.
Function Object’s
Internal property
[[Scope]]
38. function sum(x, y){
var result = x + y;
function printResult(){
console.log("foo:”+ result);
}
return printResult;
}
var print = sum(10, 20);
print();
LexicalEnvironment = {
EnvironmentRecord : {
x : 10,
y : 20,
result : undefined,
printResult : function
},
OuterEnvironmentReference :
globalEC.lexicalEnvironment
},
ThisBinding : null
sum’s EC
[[Scope]]:
printResult function
sumEC’s lexical environment
Creating Function Object
39. function sum(x, y){
var result = x + y;
function printResult(){
console.log("foo:”+ result);
}
return printResult;
}
var print = sum(10, 20);
print();
[[Scope]]:
printResult function
sumEC’s lexical environment
ThisBinding : null
sum’s EC
LexicalEnvironment = {
EnvironmentRecord : {
x : 10,
y : 20,
result : undefined,
printResult : function
},
OuterEnvironmentReference :
globalEC.lexicalEnvironment
},
Closure
40. function sum(x, y){
var result = x + y;
function printResult(){
console.log("foo:”+ result);
}
return printResult;
}
var print = sum(10, 20);
print();
[[Scope]]:
printResult function
sumEC’s lexical environment
LexicalEnvironment = {
EnvironmentRecord : {
x : 10,
y : 20,
result : undefined,
printResult : function
},
OuterEnvironmentReference :
globalEC.lexicalEnvironment
},
Closure
41. [[Scope]]: sumEC’s lexical environment
printResult function
Creating printResult’s EC
LexicalEnvironment = {
EnvironmentRecord : {
…
},
OuterEnvironmentReference :
sumEC’s lexical environment
},
ThisBinding : null
printResult’s ECfunction sum(x, y){
var result = x + y;
function printResult(){
console.log("foo:”+ result);
}
return printResult;
}
var print = sum(10, 20);
print();
46. The value of the VariableEnvironment component
never changes while the value of the
LexicalEnvironment component
may change
during execution of code within an execution context.
- 10.3 Execution Contexts in ECMA 262-5 -