Baseline Widely available
for...of
ëª
ë ¹ë¬¸ì ë°ë³µê°ë¥í ê°ì²´ (Array
, Map
, Set
, String
, TypedArray
, arguments ê°ì²´ ë±ì í¬í¨)ì ëí´ì ë°ë³µíê³ ê° ê°ë³ ìì±ê°ì ëí´ ì¤íëë ë¬¸ì´ ìë ì¬ì©ì ì ì ë°ë³µ íí¬ë¥¼ í¸ì¶íë 루í를 ìì±í©ëë¤.
const array1 = ["a", "b", "c"];
for (const element of array1) {
console.log(element);
}
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
구문
for (variable of iterable) {
statement;
}
variable
ê° ë°ë³µì ìë¡ ë¤ë¥¸ ìì±ê°ì´ variableì í ë¹ë©ëë¤.
iterable
ë°ë³µëë ì´ê±°ê°ë¥(enumerable)í ìì±ì´ ìë ê°ì²´.
Array
ì ëí´ ë°ë³µ
let iterable = [10, 20, 30];
for (let value of iterable) {
console.log(value);
}
// 10
// 20
// 30
let
ëì const
ë ì¬ì©í ì ììµëë¤, ë¸ë¡ ë´ë¶ ë³ì를 ìì íì§ ìë ê²½ì°.
let iterable = [10, 20, 30];
for (const value of iterable) {
console.log(value);
}
// 10
// 20
// 30
String
ì ëí´ ë°ë³µ
let iterable = "boo";
for (let value of iterable) {
console.log(value);
}
// "b"
// "o"
// "o"
TypedArray
ì ëí´ ë°ë³µ
let iterable = new Uint8Array([0x00, 0xff]);
for (let value of iterable) {
console.log(value);
}
// 0
// 255
Map
ì ëí´ ë°ë³µ
let iterable = new Map([
["a", 1],
["b", 2],
["c", 3],
]);
for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]
for (let [key, value] of iterable) {
console.log(value);
}
// 1
// 2
// 3
Set
ì ëí´ ë°ë³µ
let iterable = new Set([1, 1, 2, 2, 3, 3]);
for (let value of iterable) {
console.log(value);
}
// 1
// 2
// 3
DOM 컬ë ì
ì ëí´ ë°ë³µ
NodeList
ê°ì DOM 컬ë ì
ì ëí´ ë°ë³µ: ë¤ì ìë articleì ì§ê³ ììì¸ paragraphì read
í´ëì¤ë¥¼ ì¶ê°í©ëë¤:
// 주ì: ì´ë NodeList.prototype[Symbol.iterator]ê°
// 구íë íë«í¼ììë§ ìëí©ëë¤
let articleParagraphs = document.querySelectorAll("article > p");
for (let paragraph of articleParagraphs) {
paragraph.classList.add("read");
}
ìì±ê¸°ì ëí´ ë°ë³µ
ìì±ê¸°ì ëí´ìë ë°ë³µí ì ììµëë¤:
function* fibonacci() {
// ìì±ê¸° í¨ì
let [prev, curr] = [1, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (let n of fibonacci()) {
console.log(n);
// 1000ìì ìì´ì ìë¦
if (n >= 1000) {
break;
}
}
ë¤ë¥¸ ë°ë³µê°ë¥ ê°ì²´ì ëí´ ë°ë³µ
iterable íë¡í ì½ì ëª ìí´ì 구ííë ê°ì²´ì ëí´ìë ë°ë³µí ì ììµëë¤:
var iterable = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return { value: this.i++, done: false };
}
return { value: undefined, done: true };
},
};
},
};
for (var value of iterable) {
console.log(value);
}
// 0
// 1
// 2
for...of
ì for...in
ì ì°¨ì´
for...in
루íë ê°ì²´ì 모ë ì´ê±°ê°ë¥í ìì±ì ëí´ ë°ë³µí©ëë¤.
for...of
구문ì 컬ë ì
ì ì©ì
ëë¤. 모ë ê°ì²´ë³´ë¤ë, [Symbol.iterator]
ìì±ì´ ìë 모ë 컬ë ì
ììì ëí´ ì´ ë°©ìì¼ë¡ ë°ë³µí©ëë¤.
ë¤ì ìë for...of
루íì for...in
루íì ì°¨ì´ë¥¼ ë³´ì
ëë¤.
Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {};
let iterable = [3, 5, 7];
iterable.foo = "hello";
for (let i in iterable) {
console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
}
for (let i of iterable) {
console.log(i); // logs 3, 5, 7
}
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ê°ì´ 보기
Array.prototype.forEach()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4