代码示例const infos = {
name: ["koke", "james", "curry"]
}
let index = 0
const infoIterator = {
next: function () {
if (index < infos.name.length ) {
return {done:false, value: infos.name[index++]}
}
return {done:true, value: undefined}
}
}
console.log(infoIterator.next())
console.log(infoIterator.next())
console.log(infoIterator.next())
console.log(infoIterator.next())
const names = ["abc", "cba","bac"]
const nums = [100, 24, 55, 66, 86]
function createArrayIterator(arr) {
let index = 0
return {
next: function () {
if (index < arr.length) {
return {done:false, value: arr[index++]}
}
return {done: true}
}
}
}
const namesIterator = createArrayIterator(names)
const numsIterator = createArrayIterator(nums)
console.log(namesIterator.next())
console.log(namesIterator.next())
console.log(namesIterator.next())
console.log(namesIterator.next())
console.log(numsIterator.next())
console.log(numsIterator.next())
console.log(numsIterator.next())
可迭代对象的应用
- 只要在对象的属性上部署了Iterator接口,具体形式为给对象添加Symbol.iterator属性,这个属性指向一个迭代器方法,这个迭代器会返回一个特殊的对象,迭代器对象。
- 部署这个属性并且实现了迭代器方法后的对象叫做可迭代对象
- JavaScript中的语法
for…of 展开语法 yield* 解构赋值 ...
默认调用iterator接口 - 创建一些对象
new Map([Iterable]) new WeakMap([Iterable]) new Set([Iterable]) new WeakSet([Iterable]) - 一些方法的调用
Promise.all([Iterable]) Promise.race(Iterable) Array.from(Iterable) - 原生迭代器对象
String Array Map Set arguments NodeList TypedArray - 示例
for (const item of info) {
console.log(item)
}
console.log([...info, "curry"])
const [name1, name2] = info
const names = ["ab", "d","djk"]
const info = {
name: 'alsd',
dhafkl:'jsdklf'
}
function foo(arg1, arg2, arg3) {
console.log(arg1,arg2,arg3)
}
foo(...names)
const names = ["ab", "d","djk"]
const info = {
name: 'alsd',
dhafkl:'jsdklf',
[Symbol.iterator]: function () {
const entries = Object.entries(this)
let index = 0
const iterator = {
next: function (){
if (index < entries.length) {
return {done:false, value: entries[index++]}
}
return {done: true}
}
}
return iterator
}
}
function foo(arg1, arg2, arg3) {
console.log(arg1,arg2,arg3)
}
foo(...info)
const info = {
name: 'alsd',
dhafkl:'jsdklf',
[Symbol.iterator]: function () {
const entries = Object.entries(this)
let index = 0
const iterator = {
next: function (){
if (index < entries.length) {
return {done:false, value: entries[index++]}
}
return {done: true}
}
}
return iterator
}
}
function foo(arg1, arg2, arg3) {
console.log(arg1,arg2,arg3)
}
const set = new Set(["aa","bb","cc"])
for (const string of set) {
console.log(string)
}
const set1 = new Set("abc")
for (const string of set1) {
console.log(string)
}
const set2 = new Set(info)
console.log(set2)
const setIterator = set[Symbol.iterator]()
console.log(setIterator.next())
console.log(setIterator.next())
console.log(setIterator.next())
console.log(setIterator.next())
function foo() {
for (const argument of arguments) {
console.log(argument)
}
}
foo(123, 321, 111 ,222 )
迭代器的中断
迭代器在某些情况下会在没有完全迭代的情况下中断
遍历的过程中通过break ruturn throw 中断了循环操作
在结构的时候,没有解构所有的值class Person {
constructor(name, age, height, friends) {
this.name = name
this.age = age
this.height = height
this.friends = friends
}
running() {
}
[Symbol.iterator] () {
let index = 0
const iterator = {
next: () => {
if(index< this.friends.length) {
return {done: false, value: this.friends[index++]}
}
},
return: () => {
console.log("监听到迭代器中断了")
return {done: true}
},
}
return iterator
}
}
const p1 = new Person("jkld",324,234,["jsdafkl","jkasdklfj"])
for (const person of p1) {
console.log(person)
if(person === "jsdafkl") {
break
}
}