ES6—— iterator和for-of循环
Iterator 遍历器的作用:
为各种数据结构,提供一个同意的,简便的访问接口。
是的数据结构的成员能够按某种次序排列。
ES6 新增了遍历命令 for...of 循环,Iterator接口主要供 for...of 消费。
1、手写Iterator接口。
const arr=[ 1,2,3 ];
function iterator(arr){
let index=0;
return {
return index<arr.length?{value:arr[index++],done:false}:{value:undefined,done:true}
}
}
const it=iterator(arr);
console.log(it.next());
console.log(it.next());
console.log(it.next());
console.log(it.next()); ///第四条 输出 undefined
2、凡是具有 Symbol.iterator 属性的数据结构都具有 Iterator 接口。
const arr=[1,2,3];
const set=new Set(["a","b","c"]);
const map = new Map([["a":1]]);
const itArr=arr[Symbol.iterator]();
const itSetr=arr[Symbol.iterator]();
const itMap =arr[Symbol.iterator]();
console.log(itSet.next());
console.log(itSet.next());
console.log(itSet.next());
console.log(itSet.next()); ///第四条 输出 undefined
const obj = {};
console.log(obj[Symbol.iterator]); 对于空的 直接输出 undefined
3、具备iterator接口的数据结构都可以进行如下操作
结构赋值。
扩展运算符。
let str="miaov"; let arrstr=[...str]; console.log(arrstr); //输出 m.i . a.o.v const arr2=[1,"a","a","b","b"]; console.log([...new set(arr2)]) ; //输出结构为 1,a,b
4、for...of 循环
const ofArr=[1,2,3,4];
for ( let i of ofArr){
console.log(i);
}
const m = new Map();
m.set("a",1).set("b",2)
for(let[key,value] of m){
console.log(key,value);
}
//如何给一个不具备iterator结构的数据结构部署一个iterator接口?
const d={
"0":"a",
"1":"b",
"2":"c",
"3":"d",
"4":"e",
length:5
};
以上。
ES6—— iterator和for-of循环的更多相关文章
- ES6(Iterator 和 for...of 循环)
Iterator 和 for...of 循环 1.什么是 Iterator 接口 Iterator 接口功能:用一种相同办法的接口让不同的数据结构得到统一的读取命令的方式 2.Iterator的基本用 ...
- es6 -- Iterator 和 for...of 循环
1:Iterator(遍历器)的概念 JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Map和Set.这样就有了四种数据集合,用户还 ...
- es6 Iterator和for...of循环
javascript表示集合的数据结构有 es5: array object es6: map set, 一共4种数据集合 需要一种统一的接口机制来处理所有不同的数据结构 遍历器就是这样一种机制,它是 ...
- ES6 之 Set数据结构和Map数据结构 Iterator和for...of循环
ECMAScript 6 入门 Set数据结构 基本用法 ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数,用来生成Set数据结构. va ...
- es6笔记(6) Iterator 和 for...of循环
概要 js中的数组.对象,加上ES6中增加的Map.Set四种数据集合. Iterator提供了一种机制,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署Iterator接口,就可以完成遍 ...
- ES6的新特性(14)——Iterator 和 for...of 循环
Iterator 和 for...of 循环 Iterator(遍历器)的概念 JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6 又添加了Ma ...
- 14 Iterator和for...of循环
Iterator和for...of循环 首先 Iterator 是一个接口. 标准是 function makeIterator(array) { var nextIndex = 0; return ...
- 14.Iterator 和 for...of 循环
Iterator 和 for...of 循环 Iterator 和 for...of 循环 Iterator(遍历器)的概念 JavaScript 原有的表示"集合"的数据结构,主 ...
- Iterator和for...of循环
Iterator和for...of循环 Iterator(遍历器)的概念 数据结构的默认Iterator接口 调用Iterator接口的场合 字符串的Iterator接口 Iterator接口与Gen ...
- java中的Iterator与增强for循环的效率比较
最近在优化代码时遇到了这个问题:Iterator与增强for循环到底哪个效率高?之前在学习的时候,好像记着老师说过遍历集合(如list)时,使用iterator好像正规一些,因为是专用的,但是运行效率 ...
随机推荐
- dbf 命令 及数据类型
left()函数只能截取左边几位字符,截取中间字符用SUBSTR() SUBSTR(cExpression, nStartPosition [, nCharactersReturned]) dbf u ...
- PHP - 引用计数
引用计数以及是否是引用变量,一个神奇的函数,查看当前引用计数: <?php xdebug_debug_zval('a'); 以上例程会输出: a: (refcount=1, is_ref=0)= ...
- atom 配置备忘
插件 vim-mode-plus vim-mode-plus-ex-mode plateformio-ide-terminal 'cmd窗口 docblockr 帮助你快速的生成注释 linte ...
- Android5.0新动画之VectorDrawable
SVG是前端的一套标准,Vector是在Android中使用,他只是实现了SVG语言的Path的标签 Vector的常用语法 M = moveto(M X,Y): 将画笔移动到指定的坐标位置 ...
- Java中异常体系
Java中的异常体系 Throwable,是一个类,表示可抛出的,它是所有的异常都继承的类. Throwable的子类,又分为两种,一种是Error,另一种是Exception. Error一般不是应 ...
- 全文搜索技术—Solr
1. 学习计划 1. Solr的安装及配置 a) Solr整合tomcat b) Solr后台管理功能介绍 c) 配置中文分析器 2. 使用Solr的后台管理索引库 a) ...
- Android和Unity混合开发——解决方案
按这篇文章来做 http://blog.csdn.net/a369414641/article/details/53436477 要注意的地方 1.app是Android Libray,否则无法打包出 ...
- ubuntu16.04 安装opencv3
(opencvC++) luo@luo-ThinkPad-W540:20181205$ conda install --channel https://conda.anaconda.org/menpo ...
- HP发送HTTP POST请求 返回结果
HP发送HTTP POST请求 返回结果 <?php $srv_ip = '192.168.10.188';//你的目标服务地址或频道.$srv_port = 80;$url = '/demo/ ...
- C++ 模板 与 泛型编程
C++ 模板 与 泛型编程 前言 模板有两种:类模板和函数模板 .模板是泛型编程的基础. 什么叫:泛型编程? 使用独立于特定类型的方式进行编程.也就是我们在编程的时候不明确的写上类型,而是使用一个模板 ...