SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
자바스크립트의
클로져와 스코프
고대준 (kkode1911@gmail.com)
왜?
•

이벤트에 많이 사용되는 클로져.

•

Node.js는 이벤트를 이용한다. 무척 많이.

•

클로져를 이해하기 위해서는 스코프에 대한 이해가 필요.
변수
•

값 타입의 데이터
•

•

숫자, 문자열, 불린, undefined, null

참조 타입의 데이터
•

숫자, 문자열, 불린, undefined, null을 제외한 모든것
변수 범위

•

C, C++, Java등 일반적 언어에서 변수 범위는 {} 이다.

•

자바스크립트 변수 범위는 함수이다.
프로그램 실행 단계
•

프로그램
•

파싱
•

•

변수, 함수 정의

실행
•

변수 대입, 제어문, 함수호출

•

* 함수 코드 파싱
파싱 단계

•

변수와 함수를 정의한다.

•

3Line: myFun 변수가 undefined로 정의.

•

5~8Line: myFun 함수가 정의, 3Line myFunc의 변
수를 덮어쓴다.
실행 단계

•

1Line: myFun 함수가 호출, “local variable” 출력

•

3Line: myFun에 “overwrite” 대입
•

myFun이 가리키고 있던 함수는 가비지 컬렉션으로 간다.

•

10Line: “overwrite” 출력

•

메소드와 프로퍼티는 실행시 판단된다.
함수 Scope
•

함수 내부 속성에는 [[SCOPE]]라는 속성이 있다.

•

[[SCOPE]]는 우리가 접근 할 수 없다.

•

ECMA-262에서 정의
myFun
[[SCOPE]]
Scope Chain
•

함수 안의 [[SCOPE]]가 가리킨다.

•

객체 배열이다.

•

각 배열에는 변수 객체를 가리킨다.
myFun
[[SCOPE]]

Scope Chain
0
Variable Object

•

변수 항목을 키-값 쌍 형태로 포함한다.

•

함수가 만들어질때, 같이 만들어진다.
Global Object

[[SCOPE]]

Scope Chain
0

this

window

window

myFun

(object)

document

(object)

myFun

(function)

…

…
Execution Context
•

자바스크립트에서 스코프를 아리송하게 하는놈

•

파싱 단계와 실행단계의 스코프 차이를 만든다.

•

실행시 함수 스코프를 복사한후 자신만의 스코프 체인에
Activation Object을 추가한다.

•

함수 실행할 때마다 Execution Context가 만들어진다.
var result = myFun(‘closure’);
execution context

!

Scope Chain

Scope Chain
0
1

Activation Object
this
window
arguments
[name]
name
“closure”
msg
undefined

Global Object
this
window
window
(object)
document
(object)
myFun
(function)
result
undefined
…
…
퀴즈

undefined
클로져?

•

음~~~~ ㅡㅡ;

•

예제로 Go
클로져 예제
Global Object
this
window

파싱

window
(object)

document

(object)

outer

(function)

closure1

undefined

…

…
클로져 예제(1)
outer 함수의

스코프체인

Global Object
this
outer
[[SCOPE]]

Scope Chain
0

window

window

(object)

document

(object)

outer

(function)

closure1

undefined

…

…
클로져 예제(2)
8Line 실행

Activation Object
this
window
arguments
[num]
num
1
innerFun
(function)
Global Object
this

var closure1 = outer(1);
Execution Context
Scope Chain

window

window

(object)

0

document

(object)

1

outer

(function)

closure1

undefined

…

…

Scope Chain
클로져 예제(3)
innerFun의
스코프체인

var closure1 = outer(1);
Execution Context
Scope Chain

Scope Chain

[[SCOPE]]

Global Object

0

this

window

1

window

(object)

document

(object)

outer

(function)

closure1

undefined

…

…

Scope Chain
innerFun

Activation Object (outer)
this
window
arguments
[num]
num
1
innerFun
(function)

0
1
클로져 예제(4)
Activation Object (closure)
this
arguments

9Line 실행

console.log(closure1());
Execution Context
Scope Chain

Scope Chain
0
1
2

window
[]

Activation Object (outer)
this
window
arguments
[num]
num
1
innerFun
(function)
Global Object
this
window
window
(object)
document
(object)
outer
(function)
closure1
(function)
…
…
클로져 예제(5)
Activation Object (closure)
this
arguments

10Line 실행

console.log(closure1());
Execution Context
Scope Chain

Scope Chain
0
1
2

window
[]

Activation Object (outer)
this
window
arguments
[num]
num
2
innerFun
(function)
Global Object
this
window
window
(object)
document
(object)
outer
(function)
closure1
(function)
…
…
퀴즈(1)
자바스크립트의 모든 함수
는 클로져다!
참고
•

자바스크립트 객체지향 프로그래밍

•

High Performance JavaScript

•

JavaScript The Good Parts

•

JavaScript The Definitive Guide 5/E
감사합니다.

Contenu connexe

Similaire à JavaScript closure & scope

0.javascript기본(~3일차내)
0.javascript기본(~3일차내)0.javascript기본(~3일차내)
0.javascript기본(~3일차내)Sung-hoon Ma
 
C 언어 스터디 01 - 기초
C 언어 스터디 01 - 기초C 언어 스터디 01 - 기초
C 언어 스터디 01 - 기초Yu Yongwoo
 
A tour of C++ : the basics
A tour of C++ : the basicsA tour of C++ : the basics
A tour of C++ : the basicsJaewon Choi
 
엄준일 04일차 HTML/Javascript 교육
엄준일 04일차 HTML/Javascript 교육엄준일 04일차 HTML/Javascript 교육
엄준일 04일차 HTML/Javascript 교육준일 엄
 
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019min woog kim
 
Chapter5 ~ 6
Chapter5 ~ 6Chapter5 ~ 6
Chapter5 ~ 6Injae Lee
 
웹 개발 스터디 02 - javascript, bootstrap
웹 개발 스터디 02 - javascript, bootstrap웹 개발 스터디 02 - javascript, bootstrap
웹 개발 스터디 02 - javascript, bootstrapYu Yongwoo
 
Java script 기본과 jquery의 활용
Java script 기본과 jquery의 활용Java script 기본과 jquery의 활용
Java script 기본과 jquery의 활용정기 김
 
[170327 1주차]C언어 A반
[170327 1주차]C언어 A반[170327 1주차]C언어 A반
[170327 1주차]C언어 A반arundine
 
Intro to JavaScript - Week 1: Value, Type, Operator
Intro to JavaScript - Week 1: Value, Type, OperatorIntro to JavaScript - Week 1: Value, Type, Operator
Intro to JavaScript - Week 1: Value, Type, OperatorJeongbae Oh
 
제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기
제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기
제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기NAVER D2
 
Chapter7~9 ppt
Chapter7~9 pptChapter7~9 ppt
Chapter7~9 pptInjae Lee
 
Tensorflow for Deep Learning(SK Planet)
Tensorflow for Deep Learning(SK Planet)Tensorflow for Deep Learning(SK Planet)
Tensorflow for Deep Learning(SK Planet)Tae Young Lee
 
How to use the Ruby programing language
How to use the Ruby programing languageHow to use the Ruby programing language
How to use the Ruby programing languageJaeYeoul Ahn
 
Effective c++ chapter 7,8
Effective c++ chapter 7,8Effective c++ chapter 7,8
Effective c++ chapter 7,8문익 장
 
자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법Sungchul Park
 
C언어 세미나 - 함수
C언어 세미나 - 함수C언어 세미나 - 함수
C언어 세미나 - 함수SeungHyun Lee
 

Similaire à JavaScript closure & scope (20)

0.javascript기본(~3일차내)
0.javascript기본(~3일차내)0.javascript기본(~3일차내)
0.javascript기본(~3일차내)
 
C 언어 스터디 01 - 기초
C 언어 스터디 01 - 기초C 언어 스터디 01 - 기초
C 언어 스터디 01 - 기초
 
A tour of C++ : the basics
A tour of C++ : the basicsA tour of C++ : the basics
A tour of C++ : the basics
 
엄준일 04일차 HTML/Javascript 교육
엄준일 04일차 HTML/Javascript 교육엄준일 04일차 HTML/Javascript 교육
엄준일 04일차 HTML/Javascript 교육
 
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
김민욱, (달빛조각사) 엘릭서를 이용한 mmorpg 서버 개발, NDC2019
 
Chapter5 ~ 6
Chapter5 ~ 6Chapter5 ~ 6
Chapter5 ~ 6
 
웹 개발 스터디 02 - javascript, bootstrap
웹 개발 스터디 02 - javascript, bootstrap웹 개발 스터디 02 - javascript, bootstrap
웹 개발 스터디 02 - javascript, bootstrap
 
Java script 기본과 jquery의 활용
Java script 기본과 jquery의 활용Java script 기본과 jquery의 활용
Java script 기본과 jquery의 활용
 
[170327 1주차]C언어 A반
[170327 1주차]C언어 A반[170327 1주차]C언어 A반
[170327 1주차]C언어 A반
 
Intro to JavaScript - Week 1: Value, Type, Operator
Intro to JavaScript - Week 1: Value, Type, OperatorIntro to JavaScript - Week 1: Value, Type, Operator
Intro to JavaScript - Week 1: Value, Type, Operator
 
제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기
제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기
제 5회 D2 CAMPUS SEMINAR - Swift로 만든 serverframework 개발기
 
Chapter7~9 ppt
Chapter7~9 pptChapter7~9 ppt
Chapter7~9 ppt
 
Tensorflow for Deep Learning(SK Planet)
Tensorflow for Deep Learning(SK Planet)Tensorflow for Deep Learning(SK Planet)
Tensorflow for Deep Learning(SK Planet)
 
How to use the Ruby programing language
How to use the Ruby programing languageHow to use the Ruby programing language
How to use the Ruby programing language
 
Anatomy of Realm
Anatomy of RealmAnatomy of Realm
Anatomy of Realm
 
Effective c++ chapter 7,8
Effective c++ chapter 7,8Effective c++ chapter 7,8
Effective c++ chapter 7,8
 
Node.js 기본
Node.js 기본Node.js 기본
Node.js 기본
 
자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법자바에서 null을 안전하게 다루는 방법
자바에서 null을 안전하게 다루는 방법
 
C언어 세미나 - 함수
C언어 세미나 - 함수C언어 세미나 - 함수
C언어 세미나 - 함수
 
4-1. javascript
4-1. javascript4-1. javascript
4-1. javascript
 

JavaScript closure & scope