ES6 Collections Using Map, Set, WeakMap, WeakSet - Lee-hyuna/33-js-concepts-kr GitHub Wiki

ES6 Collections: Using Map, Set, WeakMap, WeakSet

์›๋ฌธ : https://www.sitepoint.com/es6-collections-map-set-weakmap-weakset/

**์ด ๊ธ€์—์„œ๋Š” 4 ๊ฐœ์˜ ์ƒˆ๋กœ์šด ES6 ์ปฌ๋ ‰์…˜๊ณผ ์ด๋“ค์ด ์ œ๊ณตํ•˜๋Š” ์žฅ์ ์„ ์‚ดํŽด ๋ด…๋‹ˆ๋‹ค. **

๋Œ€๋ถ€๋ถ„์˜ ์ฃผ์š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—๋Š” ์—ฌ๋Ÿฌ ์œ ํ˜•์˜ ๋ฐ์ดํ„ฐ ์ปฌ๋ ‰์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—๋Š” ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ ๋ฐ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Java์—๋Š” ๋ฆฌ์ŠคํŠธ, ์…‹, ๋งต, ํ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฃจ๋น„์—๋Š” ํ•ด์‹œ์™€ ๋ฐฐ์—ด์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—๋Š” ๋ฐฐ์—ด ๋งŒ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ•ต์‹ฌ์ด์—ˆ์Šต๋‹ˆ๋‹ค. ES6์—๋Š” ์–ธ์–ด์— ํž˜๊ณผ ํ‘œํ˜„๋ ฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” 4 ๊ฐ€์ง€ ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์ธ ๋งต, ์…‹, ์œ„ํฌ์…‹ ๋ฐ ์œ„ํฌ๋งต์ด ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•ด์‹œ๋งต ์ฐพ๊ธฐ

ํ•ด์‹œ ๋งต, ๋”•์…”๋„ˆ๋ฆฌ ๋ฐ ํ•ด์‹œ๋Š” ๋‹ค์–‘ํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ํ‚ค / ๊ฐ’ ์Œ์„ ์ €์žฅํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด๋ฉฐ ์ด๋Ÿฌํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋Š” ๋น ๋ฅธ ๊ฒ€์ƒ‰์„ ์œ„ํ•ด ์ตœ์ ํ™”๋ฉ๋‹ˆ๋‹ค.

ES5์—์„œ ํ‚ค์™€ ๊ฐ’์„ ๊ฐ–๋Š” ์ž„์˜์˜ ์†์„ฑ ์ปฌ๋ ‰์…˜ ์ธ JavaScript ๊ฐ์ฒด๋Š” ํ•ด์‹œ๋ฅผ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ [๊ฐ์ฒด๋ฅผ ํ•ด์‹œ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋ช‡ ๊ฐ€์ง€ ๋‹จ์ ](http://www.2ality.com/2012/01/objects -as-maps.html)์„ ๊ฐ€์ง„๋‹ค.

๋‹จ์  #1: ES5์—์„œ ํ‚ค๋Š” ๋ฌธ์ž์—ด์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

JavaScript ๊ฐ์ฒด ์†์„ฑ ํ‚ค๋Š” ๋ฌธ์ž์—ด์ด์–ด์•ผํ•˜๋ฏ€๋กœ ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ์œ ํ˜•์˜ ํ‚ค / ๊ฐ’ ์Œ ๋ชจ์Œ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ๊ธฐ๋Šฅ์ด ์ œํ•œ๋ฉ๋‹ˆ๋‹ค. ๋ฌผ๋ก  ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๋ฌธ์ž์—ด๋กœ coerce/stringifyํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ถ”๊ฐ€ ์ž‘์—…์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.

๋‹จ์  #2: ๊ฐ์ฒด๋Š” ๋ณธ์งˆ์ ์œผ๋กœ ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ฐ์ฒด๋Š” ์ปฌ๋ ‰์…˜์œผ๋กœ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค๊ณ„๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์˜ ์†์„ฑ ์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ์—†์Šต๋‹ˆ๋‹ค. (์˜ˆ๋ฅผ ๋“ค์–ด Object.keys is slow)๋ฅผ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค. ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ฐ˜๋ณตํ•˜๋ฉด ํ”„๋กœํ†  ํƒ€์ž… ์†์„ฑ๋„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. iterable ์†์„ฑ์„ ๋ชจ๋“  ๊ฐ์ฒด์— ์ถ”๊ฐ€ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ์ปฌ๋ ‰์…˜์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. forโ€ฆ in ๋ฃจํ”„์™€ hasOwnProperty()๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋Š” ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ์ผ๋ฟ์ž…๋‹ˆ๋‹ค. ๊ฐ์ฒด์˜ ์†์„ฑ์„ ๋ฐ˜๋ณต ํ•  ๋•Œ ์†์„ฑ์„ ์‚ฝ์ž… ํ•œ ์ˆœ์„œ๋Œ€๋กœ ๊ฒ€์ƒ‰ ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

๋‹จ์  #3: ๋‚ด์žฅ๋œ ๋ฉ”์†Œ๋“œ์™€ ์ถฉ๋Œ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

๊ฐ์ฒด์—๋Š” constructor, toString ๋ฐ valueOf์™€ ๊ฐ™์€ ๋‚ด์žฅ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์†์„ฑ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์†์„ฑ์œผ๋กœ ์ถ”๊ฐ€ ๋œ ๊ฒฝ์šฐ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Object.create(null)์„ ์‚ฌ์šฉํ•˜์—ฌ (object.prototype์—์„œ ์ƒ์†๋˜์ง€ ์•Š์€) ๋ฒ ์–ด(??) ๊ฐ์ฒด๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ• ๋ฟ์ž…๋‹ˆ๋‹ค.

ES6์—๋Š” ์ƒˆ๋กœ์šด ์ปฌ๋ ‰์…˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•์ด ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๋” ์ด์ƒ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋‹จ์ ์„ ๊ทน๋ณต ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

ES6์—์„œ์˜ ๋งต ์ปฌ๋ ‰์…˜ ์‚ฌ์šฉ

๋งต์€ ์šฐ๋ฆฌ๊ฐ€ ์‚ดํŽด๋ณผ ์ฒซ ๋ฒˆ์งธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ / ์ปฌ๋ ‰์…˜์ž…๋‹ˆ๋‹ค. ๋งต์€ ๋ชจ๋“  ์œ ํ˜•์˜ ํ‚ค ๋ฐ ๊ฐ’ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค. ์ƒˆ ๋งต๋ฅผ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ณ  ๊ฐ’์„ ์ถ”๊ฐ€ / ์‚ญ์ œํ•˜๊ณ  ํ‚ค / ๊ฐ’์„ ๋ฐ˜๋ณตํ•˜์—ฌ ํฌ๊ธฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘์š”ํ•œ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋งต ์ƒ์„ฑ ๋ฐ ์ผ๋ฐ˜์ ์ธ ๋ฉ”์†Œ๋“œ์˜ ์‚ฌ์šฉ

const map = new Map();
map.set('hobby', 'cycling');

const foods = { dinner: 'Curry', lunch: 'Sandwich', breakfast: 'Eggs' }; 
const normalfoods = {};

map.set(normalfoods, foods);

for (const [key, value] of map) {
  console.log(`${key} = ${value}`); 
}

map.forEach((value, key) => {
  console.log(`${key} = ${value}`);
}, map);

map.clear();
console.log(map.size === 0);

Run this example on JSBin

์…‹ ์ปฌ๋ ‰์…˜ ์‚ฌ์šฉ

์…‹์€ ์ค‘๋ณต์ด ํฌํ•จ๋˜์ง€ ์•Š์€ ์ •๋ ฌ ๋œ ๊ฐ’ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค. ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ธ๋ฑ์‹ฑ๋˜๋Š” ๋Œ€์‹  ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง‘ํ•ฉ์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ์„ธํŠธ๋Š” ์ด๋ฏธ Java, Ruby์— ์žˆ์Šต๋‹ˆ๋‹ค. -2.3.0 / libdoc / set / rdoc / Set.html), Python ๋ฐ ๊ธฐํƒ€ ์—ฌ๋Ÿฌ ์–ธ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ES6 ์„ธํŠธ์™€ ๋‹ค๋ฅธ ์–ธ์–ด์™€์˜ ํ•œ ๊ฐ€์ง€ ์ฐจ์ด์ ์€ ์ˆœ์„œ๊ฐ€ ES6์—์„œ ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค (๋‹ค๋ฅธ ๋งŽ์€ ์–ธ์–ด์—์„œ๋Š” ๊ทธ๋ ‡์ง€ ์•Š์Œ). ์ค‘์š”ํ•œ Set ๋ฉ”์†Œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

const planetsOrderFromSun = new Set();
planetsOrderFromSun.add('Mercury');
planetsOrderFromSun.add('Venus').add('Earth').add('Mars');
console.log(planetsOrderFromSun.has('Earth'));

planetsOrderFromSun.delete('Mars');
console.log(planetsOrderFromSun.has('Mars'));

for (const planet of planetsOrderFromSun) {
  console.log(planet);
}
console.log(planetsOrderFromSun.size);

planetsOrderFromSun.add('Venus');
console.log(planetsOrderFromSun.size);

planetsOrderFromSun.clear();
console.log(planetsOrderFromSun.size);

Run this example on JSBin

์œ„ํฌ ์ปฌ๋ ‰์…˜, ๋ฉ”๋ชจ๋ฆฌ, ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜

JavaScript ๊ฐ€๋น„์ง€ ์ฝœ๋ ‰์…˜์€ ๋” ์ด์ƒ ์ฐธ์กฐ๋˜์ง€ ์•Š๋Š” ๊ฐ์ฒด๊ฐ€ ์ž๋™์œผ๋กœ ์‚ญ์ œ๋˜๊ณ  ์ž์›์ด ์žฌ์ƒ๋˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ ์–‘์‹์ž…๋‹ˆ๋‹ค.

๊ฐ์ฒด์— ๋Œ€ํ•œ ๋งต ๋ฐ ์…‹์˜ ์ฐธ์กฐ๋Š” ๊ฐ•๋ ฅํ•˜๊ฒŒ ์œ ์ง€๋˜๋ฉฐ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. DOM์—์„œ ์ด๋ฏธ ์ œ๊ฑฐ ๋œ DOM ์š”์†Œ์™€ ๊ฐ™์ด ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š์€ ํฐ ๊ฐ์ฒด๋ฅผ ๋งต / ์…‹์ด ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ ๋น„์šฉ์ด ๋งŽ์ด๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ES6์—๋Š” ์œ„ํฌ๋งต๊ณผ ์œ„ํฌ์…‹์ด๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์ƒˆ๋กœ์šด ์œ„ํฌ ์ฝœ๋ ‰์…˜์ด ๋„์ž…๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ES6 ์ฝœ๋ ‰์…˜์€ ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๊ฐ์ฒด๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ง€์šธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— '์œ„ํฌ' ์ž…๋‹ˆ๋‹ค.

์œ„ํฌ๋งต

์œ„ํฌ๋งต์€ ์šฐ๋ฆฌ๊ฐ€ ๋‹ค๋ฃจ๊ณ ์žˆ๋Š” ์ƒˆ๋กœ์šด ES6 ์ปฌ๋ ‰์…˜ ์ค‘ ์„ธ ๋ฒˆ์งธ์ž…๋‹ˆ๋‹ค. ์œ„ํฌ๋งต์€ ์ผ๋ฐ˜์ ์ธ ๋งต๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๊ฐ€๋น„์ง€ ์ฝœ๋ ‰์…˜๊ณผ ๊ด€๋ จํ•˜์—ฌ ๋ฐฉ๋ฒ•์ด ์ ๊ณ  ์•ž์„œ ์–ธ๊ธ‰ ํ•œ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

const aboutAuthor = new WeakMap();
const currentAge = {};
const currentCity = {};

aboutAuthor.set(currentAge, 30);
aboutAuthor.set(currentCity, 'Denver');

console.log(aboutAuthor.has(currentCity));

aboutAuthor.delete(currentAge);

Run this example on JSBin

์‚ฌ์šฉ ์‚ฌ๋ก€

์œ„ํฌ๋งต์˜ ๋ช‡ ๊ฐ€์ง€ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์‚ฌ๋ก€๊ฐ€ ์žˆ์Œ. ๊ฐ์ฒด์˜ private ๋ฐ์ดํ„ฐ๋ฅผ private๋กœ ์œ ์ง€ํ•˜๊ณ  DOM ๋…ธ๋“œ / ๊ฐ์ฒด๋ฅผ ์ถ”์ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Private ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ ์‚ฌ๋ก€

JavaScript expert Nicholas C. Zakas ์—์„œ ์‚ฌ์šฉ ์‚ฌ๋ก€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. :

var Person = (function() {
  var privateData = new WeakMap();

  function Person(name) {
    privateData.set(this, { name: name });
  }

  Person.prototype.getName = function() {
    return privateData.get(this).name;
  };

  return Person;
}());

์—ฌ๊ธฐ์—์„œ '์œ„ํฌ๋งต'์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด์˜ ๋ฐ์ดํ„ฐ๋ฅผ private๋กœ ์œ ์ง€ํ•˜๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ๊ฐ„์†Œํ™”๋ฉ๋‹ˆ๋‹ค. Person ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํŠน์ • Person ์ธ์Šคํ„ด์Šค๊ฐ€ ์—†์œผ๋ฉด privateDataWeakMap์— ๋Œ€ํ•œ ์•ก์„ธ์Šค๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

DOM ๋…ธ๋“œ์—์„œ ์‚ฌ์šฉ ์‚ฌ๋ก€

Google Polymer ํ”„๋กœ์ ํŠธ๋Š” PositionWalker๋ผ๋Š” ์ฝ”๋“œ์—์„œ`WeakMaps '๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

PositionWalker๋Š” DOM ํ•˜์œ„ ํŠธ๋ฆฌ ๋‚ด์˜ ์œ„์น˜๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ ๋ฐ ํ•ด๋‹น ๋…ธ๋“œ ๋‚ด์˜ ์˜คํ”„์…‹์œผ๋กœ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.

์œ„ํฌ๋งต์„ ์‚ฌ์šฉํ•˜์—ฌ DOM ๋…ธ๋“œ ํŽธ์ง‘, ์ œ๊ฑฐ ๋ฐ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.

_makeClone() {
  this._containerClone = this.container.cloneNode(true);
  this._cloneToNodes = new WeakMap();
  this._nodesToClones = new WeakMap();

  ...

  let n = this.container;
  let c = this._containerClone;

  // find the currentNode's clone
  while (n !== null) {
    if (n === this.currentNode) {
    this._currentNodeClone = c;
    }
    this._cloneToNodes.set(c, n);
    this._nodesToClones.set(n, c);

    n = iterator.nextNode();
    c = cloneIterator.nextNode();
  }
}

์œ„ํฌ์…‹

์œ„ํฌ์…‹์€ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋” ์ด์ƒ ํ•„์š”ํ•˜์ง€ ์•Š์„ ๋•Œ ์š”์†Œ๋ฅผ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜ ํ•  ์ˆ˜์žˆ๋Š” ์„ธํŠธ ์ปฌ๋ ‰์…˜์ž…๋‹ˆ๋‹ค. ์œ„ํฌ์…‹์€ ๋ฐ˜๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์‚ฌ๋ก€๋Š” ๋‹ค์†Œ ์ œํ•œ์ ์ž…๋‹ˆ๋‹ค (ํ˜„์žฌ๋กœ์„œ๋Š”). ๋Œ€๋ถ€๋ถ„์˜ ์–ผ๋ฆฌ ์–ด๋‹ตํ„ฐ๋“ค์€ ์œ„ํฌ์…‹์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ํƒœ๊ทธ๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (http://stackoverflow.com/questions/30556078/ecmascript-6-what-is-weakset-for). ES6-Features.org์—๋Š” ์œ„ํฌ์…‹์—์„œ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ ๋ฐ ์‚ญ์ œํ•˜๋Š” ์˜ˆ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์ฒด๊ฐ€ ํ‘œ์‹œ๋˜์—ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.

let isMarked     = new WeakSet()
let attachedData = new WeakMap()

export class Node {
    constructor (id)   { this.id = id                  }
    mark        ()     { isMarked.add(this)            }
    unmark      ()     { isMarked.delete(this)         }
    marked      ()     { return isMarked.has(this)     }
    set data    (data) { attachedData.set(this, data)  }
    get data    ()     { return attachedData.get(this) }
}

let foo = new Node("foo")

JSON.stringify(foo) === '{"id":"foo"}'
foo.mark()
foo.data = "bar"
foo.data === "bar"
JSON.stringify(foo) === '{"id":"foo"}'

isMarked.has(foo)     === true
attachedData.has(foo) === true
foo = null  /* remove only reference to foo */
attachedData.has(foo) === false
isMarked.has(foo)     === false

๋ชจ๋“  ๊ฒƒ์„ ๋งต์œผ๋กœ ? ๋ ˆ์ฝ”๋“œ vs ES6 ์ปฌ๋ ‰์…˜

๋งต ๋ฐ ์…‹์€ ํ‚ค / ๊ฐ’ ์Œ์˜ ๋ฉ‹์ง„ ์ƒˆ ES6์˜ ์ฝœ๋ ‰์…˜์ž…๋‹ˆ๋‹ค. ์ฆ‰, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋Š” ์—ฌ๋Ÿฌ ์ƒํ™ฉ์—์„œ ์—ฌ์ „ํžˆ ์ปฌ๋ ‰์…˜์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ƒํ™ฉ์ด ์š”๊ตฌํ•˜์ง€ ์•Š๋Š” ํ•œ ์ƒˆ๋กœ์šด ES6 ๋ชจ์Œ์œผ๋กœ ๋ณ€๊ฒฝ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

[MDN๋ฌธ์„œ] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)์—์„œ๋Š” ์–ธ์ œ ๊ฐ์ฒด ๋˜๋Š” ํ‚ค ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ• ์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

  • ์ผ๋ฐ˜์ ์œผ๋กœ ๋Ÿฐํƒ€์ž„๊นŒ์ง€ ํ‚ค๋ฅผ ์•Œ ์ˆ˜ ์—†์œผ๋ฉฐ ๋™์ ์œผ๋กœ ์ฐพ์•„์•ผํ•ฉ๋‹ˆ๊นŒ?
  • ๋ชจ๋“  ๊ฐ’์˜ ์œ ํ˜•์ด ๋™์ผํ•˜๊ณ  ํ˜ธํ™˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๊นŒ?
  • ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ํ‚ค๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๊นŒ?
  • ํ‚ค-๊ฐ’ ์Œ์ด ์ข…์ข… ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ์ œ๊ฑฐ๋ฉ๋‹ˆ๊นŒ?
  • ์ž„์˜์˜ (์‰ฝ๊ฒŒ ๋ณ€๊ฒฝ๋˜๋Š”) ์–‘์˜ ํ‚ค-๊ฐ’ ์Œ์ด ์žˆ์Šต๋‹ˆ๊นŒ?
  • ์ปฌ๋ ‰์…˜์ด ๋ฐ˜๋ณต๋ฉ๋‹ˆ๊นŒ?

์ƒˆ๋กœ์šด ES6 ์ปฌ๋ ‰์…˜์œผ๋กœ ๋”์šฑ ์œ ์šฉํ•œ ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ์ƒ์„ฑ

JavaScript ์ปฌ๋ ‰์…˜์€ ์ด์ „์—๋Š” ์ƒ๋‹นํžˆ ์ œํ•œ๋˜์—ˆ์ง€๋งŒ ES6์—์„œ๋Š” ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ƒˆ๋กœ์šด ES6 ์ปฌ๋ ‰์…˜์€ ์–ธ์–ด์˜ ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ๊ณผ ์œ ์—ฐ์„ฑ์„ ์ œ๊ณต ํ• ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ์ด๋ฅผ ์ฑ„ํƒํ•œ JavaScript ๊ฐœ๋ฐœ์ž์˜ ์ž‘์—…์„ ๋‹จ์ˆœํ™”ํ•ฉ๋‹ˆ๋‹ค.


ํ•ด๋‹น ๋ถ€๋ถ„์— ๋Œ€ํ•œ ๋ฒˆ์—ญ์€ ๋ณธ๋ฌธ์˜ ๊ธ€๊ณผ ์กฐ๊ธˆ ์ƒ์ดํ•˜๊ฑฐ๋‚˜ ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์ด๋ผ ์ƒ๊ฐ์ด ๋˜์–ด ์ƒ๋žต ํ•˜์˜€์Šต๋‹ˆ๋‹ค. (์ด ๊ธ€์˜ ๊ทผ์›์ง€์™€ ๊ด€๋ จ๋œ ๋‚ด์šฉ)