首页 > 代码库 > [ES6] 19. for ... of
[ES6] 19. for ... of
In ES5, we have for ... in:
var phones = ["iPhone", "Nexus", "Nokia"];for(i in phones){ console.log(phones[i]);}//iPhone//Nexus//Nokia
What we get from for...in is index of array.
In ES6, for ... of can get value from array:
var phones = ["iPhone", "Nexus", "Nokia"];for(phone of phones){ console.log(phone);}//iPhone//Nexus//Nokia
Using for...of on object:
var es6 = { edition: 6, committee: "TC39", standard: "ECMA-262"};for (e in es6) { console.log(e);}// edition// committee// standardvar engines = Set(["Gecko", "Trident", "Webkit", "Webkit"]);for (var e of engines) { console.log(e);}// Gecko// Trident// Webkitvar es6 = new Map();es6.set("edition", 6);es6.set("committee", "TC39");es6.set("standard", "ECMA-262");
for (var [name, value] of es6) { console.log(name + ": " + value);}// edition: 6// committee: TC39// standard: ECMA-262
See more: http://javascript.ruanyifeng.com/advanced/ecmascript6.html#toc17
[ES6] 19. for ... of
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。