首页 > 其他 > 详细

For of

时间:2018-11-11 20:36:17      阅读:176      评论:0      收藏:0      [点我收藏+]

For ...............of(一个迭代属性的类似语句) 

格式:

for (variable of iterable) {

  statement}

参数:

variable

在每次迭代时,将不同属性的值分配给变量。

object

迭代其可迭代属性的对象。

例子:
let iterable = [10, 20, 30];

for (let value of iterable) {

  value += 1;

  console.log(value);}// 11// 21// 31

For in 与for of 的区别:

两个for...in和for...of语句都迭代了某些东西。它们之间的主要区别在于它们的迭代。

for...in语句以任意顺序迭代对象的可枚举属性

for...of语句迭代可迭代对象定义为迭代的数据。

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 in iterable) {

  if (iterable.hasOwnProperty(i)) {

    console.log(i); // logs 0, 1, 2, "foo"

  }}

for (let i of iterable) {

  console.log(i); // logs 3, 5, 7}

For of

原文:https://www.cnblogs.com/axl1017/p/9943361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!