MarginLon MarginLon
首页
  • Web

    • Web基础
    • Web布局
    • CodeSnippet
  • Vue

    • Vue
  • React

    • React
  • Node.js

    • Node.js
  • 技术文档
  • UI组件库
  • 动画库
  • 图形库
  • 桌面端
  • 学习笔记
  • 生活笔记
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

MarginLon

http://info.cern.ch
首页
  • Web

    • Web基础
    • Web布局
    • CodeSnippet
  • Vue

    • Vue
  • React

    • React
  • Node.js

    • Node.js
  • 技术文档
  • UI组件库
  • 动画库
  • 图形库
  • 桌面端
  • 学习笔记
  • 生活笔记
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 数据类型 类型检测 类型转换
  • 精度问题
  • 堆栈内存 函数底层运行机制 块级作用域
  • GC机制 闭包作用域 let/const/var
  • 闭包应用
  • DOM
  • BOM
  • This指向
  • 浏览器渲染机制 CRP优化 事件循环机制
  • 函数防抖节流
  • 模块化 柯里化
  • 构造函数 原型
  • CLASS
  • 工厂设计模式 深浅拷贝 深浅合并
  • Iterator/Generator
    • 1. Iterator
    • 2. Generator
  • Promise/ Async/ Await
  • HTTP/跨域
  • API
  • AJAX
  • 事件
  • 《JavaScript高级程序设计(第4版)》
MarginLon
2022-04-30
目录

Iterator/Generator

  • 1. Iterator
  • 2. Generator

# 1. Iterator

  • Iterator是一种机制,遍历各种不同的数据,依次处理成员

    • next方法遍历
    • 每次遍历返回一个对象 { done:false, value: xxx}
      • done:记录是否完成
      • value:当前遍历的结果
  • 拥有Symbol.iterator,基于for of可遍历

    • 对象默认不具备
    • 数组
    • 部分类数组 arguments/ Nodelist/ HTMLCollection...
    • String
    • Set
    • Map
    • generator object
    • ...(展开运算符)
// for of 
// 首先调用Symbol.iterator,得到一个迭代器对象itor
// 每一轮循环itor.next()
    // value 
    // done: true结束循环
arr[Symbol.iterator] = function () {
    let index = 0,
        self = this;
    return {
        next() {
            if (index > self.length - 1) {
                return {
                    done: true,
                    value: undefined
                };
            }
            let result= {
                done: false,
                value: self[index++]
            };
            return result;
        }
    }
}
//基于for...of... 遍历对象
// 法1:手动设置Symbol.iterator

// 法2: [Symbol.iterator]= Array.prototype[Symbol.iterator]

// 法3: Object.prototype[Symbol.iterator]
Object.prototype[Symbol.iterator] = function () {
    let self = this,
        keys = [
            ...Object.getOwnPropertyNames(self),
            ...Object.getOwnPropertySymbols(self)
        ],
        index = 0;
    return {
        next() {
            return index > keys.length - 1 ? {
                done: true,
                value: undefined
            } : {
                    done: false,
                    value: self[keys[index++]]
                };
        }
    };
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

# 2. Generator

// 创建函数时,如果在function后面设置 * ,则为生成器函数
//      + 生成器函数执行,函数体代表未立即执行,创建了[fn]的一个实例
//         itor.__proto__ ===fn.prototype
//      + itor符合迭代器规范
//          + next() 执行函数体代码
//          + return() 提前执行结束
//          + throw() 抛出异常
//      + Object.prototype.toString.call() => [object Generator]
//      + yield: 每次执行next(),遇到yield结束,yield的值给对象中的value;
function *fn(){

}
fn.prototype.name = "Margin";
let itor = fn();
console.log(itor);

// =====================
function* generator(){
    let x1 = yield 10;
    console.log(x1);
    let x2 = yield 20;
    console.log(x2);
    return 30;
}
let itor = generator();
// 每一次next传递的值,是给上一个yield赋值的返回值
console.log(itor.next('AA')); // 10 false   --
console.log(itor.next('BB')); // 20 false  BB
console.log(itor.next('CC')); // 30 true   CC
console.log(itor.next('DD')); // undefined true --

// =======================
function* func1() {
    yield 1;
    yield 2;
}

function* func2() {
    yield 3;
    yield* func1();
    yield 4;
}
let iterator = func2();
console.log(iterator.next()); //3
console.log(iterator.next()); //1
console.log(iterator.next()); //2
console.log(iterator.next()); //4
console.log(iterator.next()); // undefined
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
编辑 (opens new window)
上次更新: 2023/04/01, 15:26:22
工厂设计模式 深浅拷贝 深浅合并
Promise/ Async/ Await

← 工厂设计模式 深浅拷贝 深浅合并 Promise/ Async/ Await→

最近更新
01
KnockoutJS
11-12
02
综述
10-17
03
前言
10-12
更多文章>
Theme by Vdoing | Copyright © 2019-2024 MarginLon | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式