js浅显技术学习 | 青训营笔记

53 阅读2分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 4 天

迭代器

iterator 使用户在容器对象上遍访的对象,使用该接口无需关心对象的内部实现细节 在js中的标准是特定的next方法
next:一个无参数或者一个参数的函数,返回两个属性的对象
done(boolean) 如果迭代器可产生序列下一个值,则为false,如果已经将序列迭代完毕,则为true。
value :迭代器返回任何的js值,done为true都可省略

const names = ["abc", "cba", "nba"]
        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++] } }
                    else {
                        return { done: true }
                    }
                }
            }
        }
        const namesIterator = createArrayIterator(names)
        console.log(namesIterator.next())
        console.log(namesIterator.next())
        console.log(namesIterator.next())
        console.log(namesIterator.next())
        console.log(namesIterator.next())
        console.log(namesIterator.next())

可迭代对象

const infos = {
            friends: ["kobe", "james", "curry"],
            [Symbol.iterator]: function () {//计算属性名
                let index = 0
                const infosIterator = {
                    next: function () {
                        if (index < infos.friends.length) { return { done: false, value: infos.friends[index++] } }
                        else {
                            return { done: true }
                        }
                    }
                }
                return infosIterator
            }
        }
        //可迭代对象可以进行 for of 操作
        for (const item of infos) {
            console.log(item)
        }
        //可迭代对象必然有一个[Symbol.iterator]函数
        //数组就是一个可迭代对象

最终版本:迭代对象的键值对

const infos = {
            name: "kobe",
            age: 18,
            height: 1.88,
            [Symbol.iterator]: function () {//计算属性名
                //const keys=Object.keys(this)
                //const values=Object.values(this)
                const entries = Object.entries(this)
                let index = 0
                const infosIterator = {
                    next: function () {
                        if (index < entries.length) { return { done: false, value: entries[index++] } }
                        else {
                            return { done: true }
                        }
                    }
                }
                return infosIterator
            }
        }


        for (const item of infos) {
            console.log(item)
        }

原生可迭代对象:

数组 set map arguments对象 Nodelist集合 String

应用:

for of 展开语法 yield 解构 赋能

自定义类的迭代

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++] } }
                        else { return { done: true } }
                    }
                }
                return iterator
            }
        }
        const p1 = new Person("why", 18, 1.88, ["12", "22", "45"])
        for (const item of p1) {
            console.log(item)
        }

Promise

异步任务 (promise异步处理代码

function execode(callback) {
            setTimeout(() => {


                console.log("正在加载中")
                console.log("马上结束啦")
                let i = 0;
                let sum = 0;
                for (i = 0; i < 100; i++) {
                    sum++;
                }
                callback(sum)
            }, 3000)
        }
        execode((value) => {
            console.log("外界已经完成了", value)


        })

Promise是一个类 (回调数据时,可以创建一个Promise对象) 通过new 创建Promise对象时,需要传入一个回调函数,称之为executor 这个回调函数会立即执行,传入另外两个回调参数 resolve reject 调用resolve回调函数时,会执行Promise对象的then方法传入回调函数 调用reject回调函数时,会执行Promise对象的catch方法传入回调函数

//创建一个promise 对象
        const promise = new Promise((resolve, reject) => {
            //调用resolve 那么then传入的回调会被执行
            resolve("哈哈哈")
            //调用reject 那么catch传入的回调会被执行
            reject("错误") //立即执行回调函数
        })//立即执行回调函数


        promise.then(value => { console.log("成功回调") }).catch(err => { console.log("失败的回调") })

1.待定状态pending 2.兑现状态 fulfilled resolve() 3.拒绝状态rejected executor

//executor
        const promise2=new promise((resolve,reject)=>{})

promise resolve 的值

const promise2 = new Promise((resolve, reject) => {
            // 1.resolve([{ name: "mac" }, { name: "bgbg" }])普通值
            //2.如果resolve的值也是promise,则由传入的Promise决定
            //3. 如果resolve的值也对象,并且对象有then方法,那么会执行then方法根据then方法结果执行Promise状态
            promise2.then(res => { console.log("拿到", res) }).catch(err => { })