码迷,mamicode.com
首页 > 其他好文 > 详细

[ES6] 19. for ... of

时间:2015-01-02 06:26:05      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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
// standard

var engines = Set(["Gecko", "Trident", "Webkit", "Webkit"]);
for (var e of engines) {
    console.log(e);
}
// Gecko
// Trident
// Webkit

var 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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4198104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!