ES2016/ES6ã§for...of
ã追å ãããã®ã§æ¹ãã¦ãªãã¸ã§ã¯ãã®ã«ã¼ãã®æ¹æ³ãè¦ç´ããã¨æãã¾ããã
for..of
for...of
æã¯ãiterableãªãã¸ã§ã¯ãã«å¯¾ãã¦å復çãªå¦çãããã«ã¼ããä½æãã¾ãï¼iterableãªãã¸ã§ã¯ãã«ã¯çµã¿è¾¼ã¿ã®String
,Array
, ä¾ãã° Array ã«ä¼¼ãarguments
ãNodeList
ãªãã¸ã§ã¯ããTypedArray
,Map
,Set
, ã¦ã¼ã¶ã¼å®ç¾©ã®iterableãªã©ãå«ã¾ãã¾ãï¼ã
for..of 㨠for...in ã®éã
for...of
ã¯å¤ãåæããfor...in
ã¯ããããã£(ã¤ã³ããã¯ã¹)ã "prototypeã§æ¡å¼µãããããããã£ãå«ãã¦" åæãã
ex
Object.prototype.objCustomFunc = function() {}; Array.prototype.arrCustomFunc = function() {}; let arr = ['æå®®ãã¡ã', 'é§ç¢ããã', 'ç´«å¹è']; arr.unitName = 'ã½ã¬ã¤ã¦'; // for...of for (let val of arr) { console.log(val); } // => "æå®®ãã¡ã", "é§ç¢ããã", "ç´«å¹è" // for...in for (let i in arr) { console.log(i); } // => 0, 1, 2, "unitName", "arrCustomFunc", "objCustomFunc" // hasOwnProperty ã使ãã°èªèº«ã®ããããã£ã ãã«éå®ã§ãã for (let i in arr) { if ( iterable.hasOwnProperty(i) ) { console.log(i); } } // => 0, 1, 2, "unitName"
for...of ã§ãªãã¸ã§ã¯ãããã®ã¾ã¾ã«ã¼ãããããã¨ã¯ã§ããªã
Objectã¯iterableãªãã¸ã§ã¯ãã§ã¯ãªãã®ã§TypeError
ã«ãªã
let obj = { cute: 'æå®®ãã¡ã', cool: 'é§ç¢ããã', sexy: 'ç´«å¹è', pop: 'å¤æ¨¹ã¿ãã', legend: 'ç¥åç¾æ' }; for (let val of obj) { console.log(val); } // => TypeError: obj is not iterable
Object.keys()ã§ãã¼ã®é åãfor...ofã§ã«ã¼ãããã
let obj = { cute: 'æå®®ãã¡ã', cool: 'é§ç¢ããã', sexy: 'ç´«å¹è', pop: 'å¤æ¨¹ã¿ãã', legend: 'ç¥åç¾æ' }; Object.prototype.objCustomFunc = function() {}; // for...of Object.keys(obj) for (let key of Object.keys(obj)) { console.log(key, obj[key]); } // cute æå®®ãã¡ã // cool é§ç¢ããã // sexy ç´«å¹è // pop å¤æ¨¹ã¿ãã // legend ç¥åç¾æ
Array.forEachã使ãæ¹æ³
Object.keys(obj).forEach(function(key) { console.log(key, this[key]); }, obj); // cute æå®®ãã¡ã // cool é§ç¢ããã // sexy ç´«å¹è // pop å¤æ¨¹ã¿ãã // legend ç¥åç¾æ
Array.forEach
ã§å®è¡ããé¢æ°ãã¢ãã¼é¢æ°()=>
ã«ããã¨ãé¢æ°å
ã®this
ã第äºå¼æ°ã§æ¸¡ãããªãã¸ã§ã¯ãã«ãªããªãã®ã§ã第äºå¼æ°ã使ç¨ããå ´åã¯æ³¨æãå¿
è¦
for...in obj ã使ãæ¹æ³
for...in
ãªããªãã¸ã§ã¯ãã®ã¾ã¾ã«ã¼ãããããã¨ãã§ããããé
åã¨åæ§ã§prototypeã§æ¡å¼µãããããããã£ãåæããã
for (let key in obj) { console.log(key, obj[key]); } // cute æå®®ãã¡ã // cool é§ç¢ããã // sexy ç´«å¹è // pop å¤æ¨¹ã¿ãã // legend ç¥åç¾æ // objCustomFunc [Function]
æè¬ for æã§ã«ã¼ãããã
let keys = Object.keys(obj); for (let i = 0, l = keys.length; i < l; i += 1) { console.log(keys[i], obj[ keys[i] ]); } // cute æå®®ãã¡ã // cool é§ç¢ããã // sexy ç´«å¹è // pop å¤æ¨¹ã¿ãã // legend ç¥åç¾æ
ã¾ã¨ã
ãªãã¸ã§ã¯ããã«ã¼ãããããæ
for...of Object.keys(obj)
ã§ã«ã¼ããããObject.keys(obj).forEach
ã§ã«ã¼ãããã â» ã«ã¼ãä¸ã«break
ã¯ã§ããªãfor...in obj
ã§ã«ã¼ãããé©æobj.hasOwnProperty(key)
ã§èªèº«ã®ããããã£ããã§ãã¯ããfor
æã§ãªãã¸ã§ã¯ãã®ãã¼ãã¤ã³ããã¯ã¹é ã«åæãã¦ã«ã¼ãããã
ã«ã¼ãä¸ã«break
ãã§ããã®ã§ES2015/ES6ã使ããç°å¢ãªããfor...of
ã§ã«ã¼ããããã®ãä¸çªã©ã¯ããªã¨æãã¾ããã
IEãfor...of
æªå¯¾å¿ãªãããªã®ã§ãIE11å¿
é ã¿ãããªWEBãµã¤ãå¶ä½ã§ã¯babelã¨ã使ããªãã¨ãã¡ã§ãã... IE... ξ(Ô¾â¸Ô¾)ξ
[åè]
åãã¦ã®JavaScript 第3ç âES2015以éã®ææ°ã¦ã§ãéçº
- ä½è : Ethan Brown,æ¦èåºå¹¸,æ¦èãã¿
- åºç社/ã¡ã¼ã«ã¼: ãªã©ã¤ãªã¼ã¸ã£ãã³
- çºå£²æ¥: 2017/01/20
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- ãã®ååãå«ãããã°ãè¦ã
- åºç社/ã¡ã¼ã«ã¼: ããã½ããã¯
- çºå£²æ¥: 2015/10/23
- ã¡ãã£ã¢: ã¨ã¬ã¯ãããã¯ã¹
- ãã®ååãå«ãããã°ãè¦ã