Skip to Content
자바스크립트 + 리액트 디자인 패턴
book

자바스크립트 + 리액트 디자인 패턴

by 애디 오스마니(Addy Osmani), 윤창식
August 2024
Beginner to intermediate
384 pages
7h 38m
Korean
Hanbit Media, Inc.
Content preview from 자바스크립트 + 리액트 디자인 패턴
89
Chapter 07_
자바스크립트 디자인 패턴
7.3.4
WeakMap
을 사용하는 최신 모듈 패턴
ES6
에서 도입된
WeakMap
08
객체는 약한 참조를 가진 키-값의 쌍으로 이루어진 집합체입니
다. 키는 객체여야만 하나, 값으로는 뭐든지 넣을 수 있습니다.
WeakMap
객체는 기본적으로
키가 약하게 유지되는 맵
map
입니다. 그 말인 즉슨 참조되지 않는 키는 가비지 컬렉션
garbage
collection
(
GC
)의 대상이 된다는 뜻입니다. [예제
7
-
1
,
7
-
2
,
7
-
3
]에서
WeakMap
을 사용하는
모듈 패턴의 구현 예제를 볼 수 있습니다.
예제
7-1
기본 모듈 정의
let _counter = new WeakMap();
class Module {
constructor() {
_counter.set(this, 0);
}
incrementCounter() {
let counter = _counter.get(this);
counter++;
_counter.set(this, counter);
return _counter.get(this);
}
resetCounter() {
console.log(`counter value prior to reset: ${_counter.get(this)}`);
_counter.set(this, 0);
}
}
const testModule ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

AI를 위한 필수 수학

AI를 위한 필수 수학

할라 넬슨
클라우드 엔지니어를 위한 97가지 조언

클라우드 엔지니어를 위한 97가지 조언

Emily Freeman, Nathen Harvey, 정기훈(Jung Ki Hun)

Publisher Resources

ISBN: 9791169212571