码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Iterate Over Items with JavaScript's for-of Loop

时间:2017-09-14 10:26:56      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:pre   rcu   object   returns   prototype   not   objc   let   value   

In this lesson we will understand the For Of loop in Javascript which was introduced in ES6. The for-of loop lets you iterate of an itterable object (array, string, set, or map) and returns each objects value in a specified variable. This excludes plain objects as we will see in the lesson.

 

Object.prototype.objCustom = function() {}; 
Array.prototype.arrCustom = function() {};

let iterable = [3, 5, 7];
iterable.foo = ‘hello‘;

for (let i in iterable) {
  console.log(i); // 0, 1, 2, "foo", "arrCustom", "objCustom"
}

for (let i in iterable) {
  if (iterable.hasOwnProperty(i)) {
    console.log(i); // 0, 1, 2, "foo"
  }
}

for (let i of iterable) {
  console.log(i); // 3, 5, 7
}

 

"accCustom" and "objCustom" are not logged in second loop is because they are inherited. Not array and object‘s own prop.

 

[Javascript] Iterate Over Items with JavaScript's for-of Loop

标签:pre   rcu   object   returns   prototype   not   objc   let   value   

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

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