string.@@iterator
The [@@interator]()
method๋ String value์ code point๋ฅผ ๋ฐ๋ณตํ๋ new Iterator
object๋ฅผ ๋ฐํํ๊ณ ๊ฐ code point๋ฅผ string value๋ก ๋ฐํํ๋ค. ๐ง
const str = "๋ธ๋ด๋๋๋๋ ์ฝ๋ก๋๋ก ์ ํ๋ธ์ ์คํ๋ผ์ ์ ๋ น ๋ฌด๋ฃ๋ก ํ๋ฆผ";
let iterator = str[Symbol.iterator](); // StringIterator {}
let theChar = iterator.next(); // {value: "๋ธ", done: false}
while(!theChar.done && theChar.value !== ' ') {
console.log(theChar.value);
theChar = iterator.next();
// expected output: "๋ธ"
// "๋ด"
// "๋"
// "๋"
}
next() ๋ฉ์๋๋ value๋ก done(boolean)๊ณผ value๋ฅผ ๊ฐ์ง๋ค.
Syntax
str[Symbol.iterator]
Return value
new Iterator Object
Examples
Using [@@iterator]()
[@@iterator]()
surrogate pair(UTF-16 ์ฝ๋ ๋๊ฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ๋์ ๊ธ์๋ฅผ ๋ํ๋ธ ๊ฒ)๋๋ ์๊นจ์ง๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
const str = 'A\uD835\uDC68';
let strIter = str[Symbol.iterator]();
console.log(strIter.next().value); // "A"
console.log(strIter.next().value); // ๐จ (\uD835\uDC68)
Using [@@iterator]()
with for..of
[@@iterator]()
with for..offor..of ๊ตฌ๋ฌธ์ ์ปฌ๋ ์ ์ ์ฉ [Symbol.iterator]์์ฑ์ด ์๋ ๋ชจ๋ ์ปฌ๋ ์ ์์์ ๋ํด ์ด ๋ฐฉ์์ผ๋ก ๋ฐ๋ณตํ๋ค.
let iterable = 'A\uD835\uDC68B\uD835\uDC69C\uD835\uDC6A';
for (let i of iterable) {
console.log(i); //logs A, ๐จ, B, ๐ฉ, C, ๐ช
}
Browser compatibillity: IE, Opera, Safari์์ No
Last updated
Was this helpful?