์ƒˆ์†Œ์‹

Languages/Java Script

Iterator & for-of | Symbol | [Symbol.iterator()]

  • -
๋ฐ˜์‘ํ˜•

๋ชจ๋“  ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ ECMAScript6: ๋น ๋ฅด๊ฒŒ ๋ฐฐ์šฐ๋Š” ES6 ์Šคํ‚ฌ๊ณผ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋žจ ์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "๊น€ํ˜•ํƒœ"๋‹˜์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. 

์ƒ์—…์ ์ธ ์˜๋„๊ฐ€ ์•„๋‹Œ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋†“๋Š” ๋ชฉ์ ์œผ๋กœ ํฌ์ŠคํŒ… ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

www.inflearn.com/course/es6-ecmascript-6

 

ECMAScript 6: ๋น ๋ฅด๊ฒŒ ๋ฐฐ์šฐ๋Š” ES6 ์Šคํ‚ฌ๊ณผ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์ธํ”„๋Ÿฐ

๋น„๋™๊ธฐ ํ™˜๊ฒฝ์—์„œ ์‰ฝ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ES6์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ๋ฐฐ์šฐ๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์Šคํ‚ฌ์„ ํ–ฅ์ƒ์‹œ์ผœ ๋ณด์„ธ์š”. ์ดˆ๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ES6 ์˜จ๋ผ์ธ ๊ฐ•์˜ abcdefghijk

www.inflearn.com


1. Iterator & for-of

# ์ดํ„ฐ๋ ˆ์ดํ„ฐ์™€ for-of

 

๊ธฐ์กด์˜ ๋ฌธ๋ฒ•์—์„œ๋Š” ๋ฐฐ์—ด๊ณผ ๊ฐ™์€ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ˆœํ™˜ํ•  ๋•Œ iterator์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” for, for each, for -in ๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ, forEach๋Š” ๋ธŒ๋ ˆ์ดํฌ๋กœ ๋ฃจํ”„๋ฅผ ์ค‘๋‹จํ•  ์ˆ˜ ์—†์—ˆ๊ณ , for -in์€ ๊ฐ์ฒด์˜ ์†์„ฑ๋“ค์„ ์ˆœํšŒํ•  ๋•Œ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

ES6์—์„œ๋Š” ๋‹ค์–‘ํ•œ ์ˆœํ™˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋„๋ก ์ƒˆ๋กœ์šด ์ข…๋ฅ˜์˜ ๋ฃจํ”„ ๋ฌธ๋ฒ•์„ ์ถ”๊ฐ€ํ•˜์˜€๋Š”๋ฐ, ์ด๊ฒƒ์ด ๋ฐ”๋กœ iterator ๊ณผ for -of์ด๋‹ค.

 

<script>
    const myArray = [10,20,30];
    
    for(let value of myArray){
    console.log(value);
    }
</script>

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  for-ofํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฃจํ”„๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

# iterable

for-of ๊ตฌ๋ฌธ์€ ์•ž์„œ ๋ณด์•˜๋˜ ๋ฐฐ์—ด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ดํ„ฐ๋Ÿฌ๋ธ” ๊ฐ์ฒด๋ผ๋ฉด ๋ชจ๋‘ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ดํ„ฐ๋Ÿฌ๋ธ” ์ด๋ž€ ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ์ด๋ผ๋Š” ๋œป์ด๋‹ค.(์˜๋ฏธ ๊ทธ๋Œ€๋กœ)

์ดํ„ฐ๋ ˆ์ดํ„ฐ๋Š” ๊ฒฐ๊ตญ ์ดํ„ฐ๋Ÿฌ๋ธ” ๊ฐ์ฒด๋ฅผ ๋งํ•˜๋Š” ๊ฒƒ์ด๋‹ค.์ดํ„ฐ๋Ÿฌ๋ธ” ๊ฐ์ฒด๋Š” ์ˆœํšŒ๋ฅผ ์œ„ํ•ด Symbol.iterator ์†์„ฑ์„ ๊ฐ€์ง„๋‹ค.

 

์ด ์†์„ฑ์„ ์•Œ์•„๋ณด๊ธฐ ์ „์— simbol์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž

 

 

2. symbol

๊ฐ์ฒด์˜ ์–ด๋– ํ•œ ๊ฒƒ๊ณผ๋„ ๋‹ค๋ฅธ ์œ ๋‹ˆํฌํ•œ ์†์„ฑ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ํƒ€์ž…

<script>
  let name = Symbol("kim");
  console.log(typeof name);
</script>

์‹ฌ๋ณผ์€ ์œ„์™€ ๊ฐ™์ด ์ƒ์„ฑํ•˜๋ฉฐ,  ๋‹จํ•ญ์—ฐ์‚ฐ์ž typeof์˜ ๊ฒฐ๊ณผ๋กœ ๊ธฐ๋ณธํ˜•์ธ "Symbol"์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

typeof๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ํƒ€์ž…

  • Undefined
  • Boolean
  • String
  • Number
  • Object
  • Function
  • Symbol

object์™€ function์„ ์ œ์™ธํ•˜๋ฉด ๋ชจ๋‘ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธํ˜•์ด๋‹ค.๊ธฐ๋ณธํ˜•์—๋Š” null๋„ ์žˆ์ง€๋งŒ, typeof null์€ object๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

<script>
const myObject = {
	symbol1: [Symbol("My Symbol")],
    symbol2: [Symbol("My Symbol")],
    };
console.log(myObject.symbol1 == myObject.symbol2);
</script>

 

๊ฒฐ๊ณผ๋Š” false์ด๋‹ค.

 

Symbol์˜ ๊ฐ€์žฅ ํฐ ํŠน์ง•์€ ๊ณ ์œ ํ•œ ๊ฒƒ์ด๋‹ค. myObject ๊ฐ์ฒด๋Š” symbol1,2 ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ๊ฐ value๋‚ด๋ถ€์— "My Symbol"์ด๋ผ๋Š” ๊ฐ™์€ description์„ ๊ฐ–๊ณ  ์žˆ์ง€๋งŒ, Symbol์€ ๊ณ ์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๋Š” false๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

๐Ÿ‘‰๐Ÿป Symbol ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ธฐ 

developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Symbol

 

Symbol - JavaScript | MDN

Symbol() ํ•จ์ˆ˜๋Š” ์‹ฌ๋ณผ(symbol) ํ˜•์‹์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ด ์‹ฌ๋ณผ์€ ๋‚ด์žฅ ๊ฐ์ฒด(built-in objects)์˜ ์—ฌ๋Ÿฌ ๋ฉค๋ฒ„๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ •์  ํ”„๋กœํผํ‹ฐ์™€ ์ „์—ญ ์‹ฌ๋ณผ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ(global symbol registry)๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ •์  ๋ฉ”์„œ๋“œ

developer.mozilla.org

 

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์ž

<script>
const myObject = {};

myObject.a = "apple";
myObject["b"] = "book";

let c = Symbol("c");
myObject[c] = "cat"
myObject[Symbol("d")] = "dog";

for(let key in Object){
	console.log("key in : " + key);
}
console.log("Object.keys(myObject): " + Object.keys(myObject));
</script>

๊ฐ์ฒด์— ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ 3๋ฒˆ,4๋ฒˆ(script์ œ์™ธํ•˜๊ณ ) ๋ผ์ธ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ 7๋ฒˆ,8๋ฒˆ ๋ผ์ธ์˜ ์‹ฌ๋ณผ์€ ๋”๋ธ” ์ฟผํ…Œ์ด์…˜์œผ๋กœ ๊ฐ์‹ธ์ง€ ์•Š๋Š”๋‹ค.

์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ๋ฃจํ”„์ธ for-in์ด๋‚˜ Object.keys๋ฉ”์†Œ๋“œ๋Š” ์‹ฌ๋ณผ ํ‚ค๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ธฐ ๋•Œ๋ฌธ์— ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

์ฝ”๋“œ ์•„๋ž˜์˜ ์ƒˆ๋กœ์šด api๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž

<script>
const myObject = {};

myObject.a = "apple";
myObject["b"] = "book";

let c = Symbol("c");
myObject[c] = "cat"
myObject[Symbol("d")] = "dog";

for(let key in Object){
	console.log("key in : " + key);
}
console.log("Object.keys(myObject): " + Object.keys(myObject));
console.log("myObject[c] : " + myObject[c]);
console.log(Object.getOwnPropertySymbols(myObject));
</script>

 

๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

์ƒˆ๋กœ์šด api "Object.getOwnpropertySymbols()" ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด ์•ˆ์— ์กด์žฌํ•˜๋Š” ์‹ฌ๋ณผ ํ‚ค ๋ชฉ๋ก์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ์‹ฌ๋ณผ ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ž์ฒด์ ์ธ ํ‘œ์ค€์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ๋Š” ์‹ฌ๋ณผ์ด ๋ช‡ ๊ฐœ ์žˆ๋Š”๋ฐ ๊ทธ ์ค‘ ํ•˜๋‚˜๊ฐ€ Symbol.iterator์ด๋‹ค.

 

๐Ÿ‘‰๐Ÿป ๊ฐ ์†์„ฑ ์‚ดํŽด๋ณด๊ธฐ 

pks2974.medium.com/javascript์™€-์‹ฌ๋ณผ-symbol-bbdf3251aa28

 

Javascript์™€ ์‹ฌ๋ณผ Symbol

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

pks2974.medium.com

์ด ์‹ฌ๋ณผ์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋ฉด ์ด ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ” ํ•˜๋‹ค ๋ผ๊ณ  ์ •์˜๋œ ๊ฒƒ์ด๋‹ค. ๊ฒฐ๊ตญ ์ด ๊ฐ์ฒด๋Š” for-of ๊ตฌ๋ฌธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

 

 

3. simbol.iterator()

 

# for-of์˜ ๋‚ด๋ถ€๋™์ž‘

์ดํ„ฐ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๋Š” ์•ž์„œ ์‚ดํŽด๋ณธ Symbol.iterator ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

for-of ๋ฃจํ”„๋Š” ๋ฐ”๋กœ ์ด symbol.iterator ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋กœ ์‹œ์ž‘์ด ๋˜๊ณ , ๊ทธ ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ ์ดํ„ฐ๋ ˆ์ดํ„ฐ ๊ฐ์ฒด๊ฐ€ ๋ฆฌํ„ด๋œ๋‹ค.

 

# iterator

- next()๋ฉ”์„œ๋“œ์™€ value, done์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง„๋‹ค.

- next ๋ฉ”์„œ๋“œ๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ปฌ๋ ‰์…˜์˜ ๊ฐ์ฒด๋“ค์„ ์ˆœํšŒํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

<script>
const set = new Set([10,20]);

const myIter = set.entries();
console.log(myIter.next());
console.log(myIter.next());
console.log(myIter.next());
</script>

 

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

//set์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋‹ค์Œ ๊ฐ•์ขŒ์—์„œ ๋‹ค๋ฃจ๊ฒ ๋‹ค.

 

myIter๋ผ๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ๊ฐ€ ์ค€๋น„๋˜์–ด์žˆ๋‹ค. ์ดํ„ฐ๋ ˆ์ดํ„ฐ๊ฐ€ ๊ฐ€์ง„ next๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ value์™€ done์†์„ฑ์„ ๊ฐ€์ง„ ๊ฐ์ฒด๊ฐ€ ๋ฆฌํ„ด๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Value๋Š” ํ˜„์žฌ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ณณ์˜ ๊ฐ’์ด๊ณ , done์€ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๊ฐ€ ๋๊นŒ์ง€ ๋„๋‹ฌํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ true, false๋กœ ์•Œ๋ ค์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

๊ฒฐ๊ณผ์˜ ๋งจ ๋งˆ์ง€๋ง‰๊ณผ ๊ฐ™์ด ๋๊นŒ์ง€ ๊ฐ€๊ฒŒ๋˜๋ฉด value๋Š” undefined, done์€ true๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

# ์ •๋ฆฌํ•˜๊ธฐ

- for-of ๊ตฌ๋ฌธ์ด ์ž‘๋™ํ•˜๋Š” ๋ฐฉ์‹์€ ๋‹ค๋ฅธ ์–ธ์–ด๋“ค์˜ for-of ๊ตฌ๋ฌธ๋“ค ์ฒ˜๋Ÿผ ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ค ๋ฉ”์†Œ๋“ค์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.

- ES6์—์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ for-of ๊ตฌ๋ฌธ์€ ๋ชจ๋“  iterable ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•

'Languages > Java Script' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Generator & yield | yield* | return  (0) 2021.02.15
Rest Parameter | Map | Maplterator | Filtering & Set | Set  (0) 2021.02.15
Arrow Function & this | IIFE | Default Parameter  (0) 2021.02.15
Template Literals | Destructuring | class  (0) 2021.02.15
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.