SlideShare une entreprise Scribd logo
1  sur  51
By 김훈민
NHN Technology Services Corp.
Front-End Development Team
http://huns.me
Lexical
Environment
in ECMA-262 Edition 5.1
ECMA-262 Edition 5, 2009
ECMA-262 Edition 3 탄생 이후 10년
JSON
Function
Object
Arrays
use strict
Date
New Features
in ECMA-262 Edition 5
1 more thing.
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
Before ECMA 262-5…?
Variable Object
Chapter 10.
Execution Context
ECMAScript 코드를 실행하는 환경
Execution Context
<script>
var sayHello = "Hello";
function person(){
var first = 'David';
function getFirstName(){
eval("first = 'Steven'");
return first;
}
alert(sayHello + getFirstName());
}
person();
</script>
Execution Context
Execution Context Stack
Execution Context
Execution Context
Execution Context
Execution Context
Executable Code
<script>
var sayHello = "Hello";
function person(){
var first = 'David';
function getFirstName(){
eval("first = 'Steven'");
return first;
}
alert(sayHello + getFirstName());
}
person();
</script>
Function
Code
Global
Code
Eval
Code
Evaluating
Initializing
execution context
Initializing
execution context
Initializing
execution context
Pseudo code
ExecutionContext = {
LexicalEnvironment : [Lexical Environment],
VariableEnvironment : [Lexical Environment],
ThisBinding : [object]
}
* […]은 Value Type
Lexical Environment
자원을 어디에서 찾을 것인가
Components of Lexical Environment
lexical environment = {
environment record : -,
outer environment reference : -
}
지역 식별자 덩어리
유효범위 내의 식별자를 바인딩
중첩 유효범위를 가질 수 있는 환경에
서 상위 lexical environment를 참조
2 type of environment record
Declarative
vs
Object
DeclarativeEnvironmentRecord : {
x : 10,
y : 20,
result : 30
}
ObjectEnvironmentRecord : {
bindObject : globalObject
}
Establishing
an Execution Context
실행 콘텍스트 초기화
thisBinding, Hoisting
Global Code
{
LexicalEnvironment : globalEnv,
VariableEnvironment : globalEnv,
ThisBinding : window
}
Global Execution Context
ObjectEnvironmentRecord : {
bindingObject : window
},
OuterEnvironmentReferece : null,
globalEnv
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
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
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
thisBinding on Function
sunFunctionEC.ThisBinding = foo;foo.sum(10, 20);
{
ThisBinding : foo,
}
sumFunctionEC
{
ThisBinding : null,
LexicalEnv : -
}
sumFunctionEC
EnvironmentRecord : {}
localEnv
localEnv
Creating local lexical environment
Declaration Binding
Instantiation
실행 콘텍스트 바인딩 초기화
function sum(x, y){
console.log(barr);
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
{
ThisBinding : null,
LexicalEnv : localEnv
}
sumFunctionEC
EnvironmentRecord : {
}
x : 10,
y : 20,
localEnv
Declaration Binding Instantiation
EnvironmentRecord : {
x : 10,
y : 20,
}
function sum(x, y){
console.log(barr);
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
printResult : Function Reference
Hoisting
{
ThisBinding : null,
LexicalEnv : localEnv
}
sumFunctionEC
localEnv
Declaration Binding Instantiation
function sum(x, y){
console.log(barr);
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
EnvironmentRecord : {
x : 10,
y : 20,
printResult : Function Reference,
}
arguments : Arguments Object
{
ThisBinding : null,
LexicalEnv : localEnv
}
sumFunctionEC
localEnv
Declaration Binding Instantiation
EnvironmentRecord : {
x : 10,
y : 20,
printResult : Function Reference
}
function sum(x, y){
console.log(barr);
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
result : undefined,
barr : undefined
Hoisting
{
ThisBinding : null,
LexicalEnv : localEnv
}
sumFunctionEC
localEnv
Declaration Binding Instantiation
EnvironmentRecord : {
x : 10,
y : 20,
printResult : Function Reference,
result : undefined,
barr : undefined
}
function sum(x, y){
console.log(barr);
var result = x + y;
var barr = function(){
console.log("barr");
}
function printResult(){
console.log(result);
}
return printResult;
}
sum(10, 20);
{
ThisBinding : null,
LexicalEnv : localEnv
}
sumFunctionEC
localEnv
Declaration Binding Instantiation
function sum(x, y){
console.log(barr);
var result = x + y;
var barr = function(){
console.log("barr");
}
function foo(){
console.log("foo");
}
return result;
}
sum(10, 20);
result : 30,
barr : Function Reference
{
ThisBinding : null,
LexicalEnv : localEnv
}
sumFunctionEC
EnvironmentRecord : {
x : 10,
y : 20,
foo : Function Reference
result : undefined,
barr : undefined
}
localEnv
Declaration Binding Instantiation
result : 30,
barr : Function Reference
Identifier Resolution
식별자와 묶여있는 값을 찾아가는 과정
스코프 체인의 원리
outer environment reference
The outer environment reference is used
to model the logical nesting of Lexical Environment values
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
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
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
Closure
자유변수를 간직한 코드 블럭
A lexical environment that defines the environment
in which a Function object is executed.
Function Object’s
Internal property
[[Scope]]
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
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
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
[[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();
VariableEnvironment
LexicalEnvironment의 쌍둥이
Execution Context Pseudo code
ExecutionContext = {
LexicalEnvironment : [lexical environment],
VariableEnvironment : [lexical environment],
ThisBinding : [object]
}
* […]은 type
LexicalEnvironment and VariableEnvironment
components of an execution context
are always Lexical Environments
- 10.3 Execution Contexts in ECMA 262-5 -
VariableEnvironment = LexicalEnvironment
{
ThisBinding : null,
LexicalEnv : globalEnv,
VariableEnv : null
}
Global EC
EnvironmentRecord : {
bindingObject : window
},
OuterEnviromentReferece : null
globalEnv
var foo = "abc";
with({ foo : "bar" }) {
function f() {
console.log(foo);
}
f();
}
GlobalEC.VariableEnv = GlobalEC.LexicalEnv;
globalEnv
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 -
With Statement #1
{
ThisBinding : null,
LexicalEnv : globalEnv,
VariableEnv : globalEnv
}
Global EC
EnvironmentRecord : {
bindingObject : window
},
OuterEnviromentReferece : null
globalEnv
var foo = "abc";
with({ foo : "bar" }) {
function f() {
console.log(foo);
}
f();
}
ObjectEnvironmentRecord : {
bindingObject : -
},
OuterEnviromentReferece :
newEnv
{foo:bar}
newEnv,
GlobalEC.LexicalEnv
With Statement #2
{
ThisBinding : null,
LexicalEnv : newEnv,
VariableEnv : globalEnv
}
Global EC
EnvironmentRecord : {
bindingObject : window
},
OuterEnviromentReferece : null
globalEnv
var foo = "abc";
with({ foo : "bar" }) {
function f() {
console.log(foo);
}
f();
}
ObjectEnvironmentRecord : {
bindingObject : { foo : bar }
},
OuterEnviromentReferece :
GlobalEC.LexicalEnv
newEnv
With Statement #3
{
ThisBinding : null,
LexicalEnv : newEnv,
VariableEnv : globalEnv
}
Global EC
EnvironmentRecord : {
bindingObject : window
},
OuterEnviromentReferece : null
globalEnv
ObjectEnvironmentRecord : {
bindingObject : { foo : bar }
},
OuterEnviromentReferece : null
newEnv
globalEnv
with문이 끝나면
GlobalEC.LexicalEnv = GlobalEC.VariableEnv;
Reference
1. ECMAScript Language Specification 5.1 Edition
2. ECMAScript 5 spec: LexicalEnvironment versus VariableEnvironment
3. ECMA-262-5 in detail. Chapter 3.2 Lexical environments
Thanks.
email : kim.hunmin@nhn.com
facebook : facebook.com/jeokrang
blog : http://huns.me

Contenu connexe

Tendances

Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptSeok-joon Yun
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」matuura_core
 
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуdelimitry
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_functiontimotheeg
 
Refactoring group 1 - chapter 3,4,6
Refactoring   group 1 - chapter 3,4,6Refactoring   group 1 - chapter 3,4,6
Refactoring group 1 - chapter 3,4,6Duy Lâm
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9Andrey Zakharevich
 
Javascript basics
Javascript basicsJavascript basics
Javascript basicsFin Chen
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
 
Type Driven Development with TypeScript
Type Driven Development with TypeScriptType Driven Development with TypeScript
Type Driven Development with TypeScriptGarth Gilmour
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation JavascriptRamesh Nair
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Wilson Su
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeCory Forsyth
 
The Ring programming language version 1.5.1 book - Part 74 of 180
The Ring programming language version 1.5.1 book - Part 74 of 180The Ring programming language version 1.5.1 book - Part 74 of 180
The Ring programming language version 1.5.1 book - Part 74 of 180Mahmoud Samir Fayed
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges✅ William Pinaud
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)Yuki Tamura
 

Tendances (20)

Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」連邦の白いヤツ 「Objective-C」
連邦の白いヤツ 「Objective-C」
 
ES6 in Real Life
ES6 in Real LifeES6 in Real Life
ES6 in Real Life
 
EcmaScript 6
EcmaScript 6 EcmaScript 6
EcmaScript 6
 
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему кодуITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
ITGM #9 - Коварный CodeType, или от segfault'а к работающему коду
 
The mighty js_function
The mighty js_functionThe mighty js_function
The mighty js_function
 
Refactoring group 1 - chapter 3,4,6
Refactoring   group 1 - chapter 3,4,6Refactoring   group 1 - chapter 3,4,6
Refactoring group 1 - chapter 3,4,6
 
Коварный code type ITGM #9
Коварный code type ITGM #9Коварный code type ITGM #9
Коварный code type ITGM #9
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 
Type Driven Development with TypeScript
Type Driven Development with TypeScriptType Driven Development with TypeScript
Type Driven Development with TypeScript
 
Travel management
Travel managementTravel management
Travel management
 
ECMAScript 6
ECMAScript 6ECMAScript 6
ECMAScript 6
 
ES6 - Next Generation Javascript
ES6 - Next Generation JavascriptES6 - Next Generation Javascript
ES6 - Next Generation Javascript
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
Explaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to ComeExplaining ES6: JavaScript History and What is to Come
Explaining ES6: JavaScript History and What is to Come
 
The Ring programming language version 1.5.1 book - Part 74 of 180
The Ring programming language version 1.5.1 book - Part 74 of 180The Ring programming language version 1.5.1 book - Part 74 of 180
The Ring programming language version 1.5.1 book - Part 74 of 180
 
PHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP LimogesPHP in 2018 - Q4 - AFUP Limoges
PHP in 2018 - Q4 - AFUP Limoges
 
C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)C++の話(本当にあった怖い話)
C++の話(本当にあった怖い話)
 

Similaire à Lexical environment in ecma 262 5

JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervosoLuis Vendrame
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka RemotingKnoldus Inc.
 
Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with JavascriptDeepankar Chopra
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSAdam L Barrett
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suckerockendude
 
Un dsl pour ma base de données
Un dsl pour ma base de donnéesUn dsl pour ma base de données
Un dsl pour ma base de donnéesRomain Lecomte
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchainedEduard Tomàs
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in GolangBo-Yi Wu
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 
Call stack, event loop and async programming
Call stack, event loop and async programmingCall stack, event loop and async programming
Call stack, event loop and async programmingMasters Academy
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to JavascriptAnjan Banda
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaWiem Zine Elabidine
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScriptChengHui Weng
 

Similaire à Lexical environment in ecma 262 5 (20)

JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
 
Java scriptconfusingbits
Java scriptconfusingbitsJava scriptconfusingbits
Java scriptconfusingbits
 
Java scriptconfusingbits
Java scriptconfusingbitsJava scriptconfusingbits
Java scriptconfusingbits
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka Remoting
 
Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with Javascript
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
 
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
Es6 hackathon
Es6 hackathonEs6 hackathon
Es6 hackathon
 
Orlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't SuckOrlando BarCamp Why Javascript Doesn't Suck
Orlando BarCamp Why Javascript Doesn't Suck
 
Un dsl pour ma base de données
Un dsl pour ma base de donnéesUn dsl pour ma base de données
Un dsl pour ma base de données
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
Call stack, event loop and async programming
Call stack, event loop and async programmingCall stack, event loop and async programming
Call stack, event loop and async programming
 
Intro to Javascript
Intro to JavascriptIntro to Javascript
Intro to Javascript
 
ZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in ScalaZIO: Powerful and Principled Functional Programming in Scala
ZIO: Powerful and Principled Functional Programming in Scala
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
ES6, WTF?
ES6, WTF?ES6, WTF?
ES6, WTF?
 

Plus de Kim Hunmin

React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기Kim Hunmin
 
웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기
웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기
웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기Kim Hunmin
 
Facebook은 React를 왜 만들었을까?
Facebook은 React를 왜 만들었을까? Facebook은 React를 왜 만들었을까?
Facebook은 React를 왜 만들었을까? Kim Hunmin
 
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple OverviewKim Hunmin
 
Why javaScript?
Why javaScript?Why javaScript?
Why javaScript?Kim Hunmin
 
Dom selecting & jQuery
Dom selecting & jQueryDom selecting & jQuery
Dom selecting & jQueryKim Hunmin
 

Plus de Kim Hunmin (6)

React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기React로 TDD 쵸큼 맛보기
React로 TDD 쵸큼 맛보기
 
웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기
웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기
웹 프론트엔드 개발자의 얕고 넓은 Rx 이야기
 
Facebook은 React를 왜 만들었을까?
Facebook은 React를 왜 만들었을까? Facebook은 React를 왜 만들었을까?
Facebook은 React를 왜 만들었을까?
 
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple Overview
 
Why javaScript?
Why javaScript?Why javaScript?
Why javaScript?
 
Dom selecting & jQuery
Dom selecting & jQueryDom selecting & jQuery
Dom selecting & jQuery
 

Lexical environment in ecma 262 5

  • 1. By 김훈민 NHN Technology Services Corp. Front-End Development Team http://huns.me Lexical Environment in ECMA-262 Edition 5.1
  • 2. ECMA-262 Edition 5, 2009 ECMA-262 Edition 3 탄생 이후 10년
  • 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
  • 7. Chapter 10. Execution Context ECMAScript 코드를 실행하는 환경
  • 8. Execution Context <script> var sayHello = "Hello"; function person(){ var first = 'David'; function getFirstName(){ eval("first = 'Steven'"); return first; } alert(sayHello + getFirstName()); } person(); </script> Execution Context
  • 9. Execution Context Stack Execution Context Execution Context Execution Context Execution Context
  • 10. Executable Code <script> var sayHello = "Hello"; function person(){ var first = 'David'; function getFirstName(){ eval("first = 'Steven'"); return first; } alert(sayHello + getFirstName()); } person(); </script> Function Code Global Code Eval Code Evaluating Initializing execution context Initializing execution context Initializing execution context
  • 11. Pseudo code ExecutionContext = { LexicalEnvironment : [Lexical Environment], VariableEnvironment : [Lexical Environment], ThisBinding : [object] } * […]은 Value Type
  • 13. Components of Lexical Environment lexical environment = { environment record : -, outer environment reference : - } 지역 식별자 덩어리 유효범위 내의 식별자를 바인딩 중첩 유효범위를 가질 수 있는 환경에 서 상위 lexical environment를 참조
  • 14. 2 type of environment record Declarative vs Object
  • 15. DeclarativeEnvironmentRecord : { x : 10, y : 20, result : 30 }
  • 17. Establishing an Execution Context 실행 콘텍스트 초기화 thisBinding, Hoisting
  • 18. Global Code { LexicalEnvironment : globalEnv, VariableEnvironment : globalEnv, ThisBinding : window } Global Execution Context ObjectEnvironmentRecord : { bindingObject : window }, OuterEnvironmentReferece : null, globalEnv
  • 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
  • 22. thisBinding on Function sunFunctionEC.ThisBinding = foo;foo.sum(10, 20); { ThisBinding : foo, } sumFunctionEC
  • 23. { ThisBinding : null, LexicalEnv : - } sumFunctionEC EnvironmentRecord : {} localEnv localEnv Creating local lexical environment
  • 25. function sum(x, y){ console.log(barr); var result = x + y; var barr = function(){ console.log("barr"); } function printResult(){ console.log(result); } return printResult; } sum(10, 20); { ThisBinding : null, LexicalEnv : localEnv } sumFunctionEC EnvironmentRecord : { } x : 10, y : 20, localEnv Declaration Binding Instantiation
  • 26. EnvironmentRecord : { x : 10, y : 20, } function sum(x, y){ console.log(barr); var result = x + y; var barr = function(){ console.log("barr"); } function printResult(){ console.log(result); } return printResult; } sum(10, 20); printResult : Function Reference Hoisting { ThisBinding : null, LexicalEnv : localEnv } sumFunctionEC localEnv Declaration Binding Instantiation
  • 27. function sum(x, y){ console.log(barr); var result = x + y; var barr = function(){ console.log("barr"); } function printResult(){ console.log(result); } return printResult; } sum(10, 20); EnvironmentRecord : { x : 10, y : 20, printResult : Function Reference, } arguments : Arguments Object { ThisBinding : null, LexicalEnv : localEnv } sumFunctionEC localEnv Declaration Binding Instantiation
  • 28. EnvironmentRecord : { x : 10, y : 20, printResult : Function Reference } function sum(x, y){ console.log(barr); var result = x + y; var barr = function(){ console.log("barr"); } function printResult(){ console.log(result); } return printResult; } sum(10, 20); result : undefined, barr : undefined Hoisting { ThisBinding : null, LexicalEnv : localEnv } sumFunctionEC localEnv Declaration Binding Instantiation
  • 29. EnvironmentRecord : { x : 10, y : 20, printResult : Function Reference, result : undefined, barr : undefined } function sum(x, y){ console.log(barr); var result = x + y; var barr = function(){ console.log("barr"); } function printResult(){ console.log(result); } return printResult; } sum(10, 20); { ThisBinding : null, LexicalEnv : localEnv } sumFunctionEC localEnv Declaration Binding Instantiation
  • 30. function sum(x, y){ console.log(barr); var result = x + y; var barr = function(){ console.log("barr"); } function foo(){ console.log("foo"); } return result; } sum(10, 20); result : 30, barr : Function Reference { ThisBinding : null, LexicalEnv : localEnv } sumFunctionEC EnvironmentRecord : { x : 10, y : 20, foo : Function Reference result : undefined, barr : undefined } localEnv Declaration Binding Instantiation result : 30, barr : Function Reference
  • 31. Identifier Resolution 식별자와 묶여있는 값을 찾아가는 과정 스코프 체인의 원리
  • 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();
  • 43. Execution Context Pseudo code ExecutionContext = { LexicalEnvironment : [lexical environment], VariableEnvironment : [lexical environment], ThisBinding : [object] } * […]은 type
  • 44. LexicalEnvironment and VariableEnvironment components of an execution context are always Lexical Environments - 10.3 Execution Contexts in ECMA 262-5 -
  • 45. VariableEnvironment = LexicalEnvironment { ThisBinding : null, LexicalEnv : globalEnv, VariableEnv : null } Global EC EnvironmentRecord : { bindingObject : window }, OuterEnviromentReferece : null globalEnv var foo = "abc"; with({ foo : "bar" }) { function f() { console.log(foo); } f(); } GlobalEC.VariableEnv = GlobalEC.LexicalEnv; globalEnv
  • 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 -
  • 47. With Statement #1 { ThisBinding : null, LexicalEnv : globalEnv, VariableEnv : globalEnv } Global EC EnvironmentRecord : { bindingObject : window }, OuterEnviromentReferece : null globalEnv var foo = "abc"; with({ foo : "bar" }) { function f() { console.log(foo); } f(); } ObjectEnvironmentRecord : { bindingObject : - }, OuterEnviromentReferece : newEnv {foo:bar} newEnv, GlobalEC.LexicalEnv
  • 48. With Statement #2 { ThisBinding : null, LexicalEnv : newEnv, VariableEnv : globalEnv } Global EC EnvironmentRecord : { bindingObject : window }, OuterEnviromentReferece : null globalEnv var foo = "abc"; with({ foo : "bar" }) { function f() { console.log(foo); } f(); } ObjectEnvironmentRecord : { bindingObject : { foo : bar } }, OuterEnviromentReferece : GlobalEC.LexicalEnv newEnv
  • 49. With Statement #3 { ThisBinding : null, LexicalEnv : newEnv, VariableEnv : globalEnv } Global EC EnvironmentRecord : { bindingObject : window }, OuterEnviromentReferece : null globalEnv ObjectEnvironmentRecord : { bindingObject : { foo : bar } }, OuterEnviromentReferece : null newEnv globalEnv with문이 끝나면 GlobalEC.LexicalEnv = GlobalEC.VariableEnv;
  • 50. Reference 1. ECMAScript Language Specification 5.1 Edition 2. ECMAScript 5 spec: LexicalEnvironment versus VariableEnvironment 3. ECMA-262-5 in detail. Chapter 3.2 Lexical environments
  • 51. Thanks. email : kim.hunmin@nhn.com facebook : facebook.com/jeokrang blog : http://huns.me