SlideShare a Scribd company logo
자바스크립트의
클로져와 스코프
고대준 (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
감사합니다.

More Related Content

PDF
C++ Advanced 강의 5주차
PPTX
Hoons 닷넷 정기세미나
PDF
C++ Advanced 강의 3주차
PPTX
Modern C++의 타입 추론과 람다, 컨셉
PPT
호이스팅, 클로저, IIFE
PDF
C++ Advanced 강의 소개
PDF
C++ Advanced 강의 1주차
PDF
C++’s move semantics
C++ Advanced 강의 5주차
Hoons 닷넷 정기세미나
C++ Advanced 강의 3주차
Modern C++의 타입 추론과 람다, 컨셉
호이스팅, 클로저, IIFE
C++ Advanced 강의 소개
C++ Advanced 강의 1주차
C++’s move semantics

Similar to JavaScript closure & scope (20)

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

JavaScript closure & scope