Web全栈20210321-es6遍历器,异步编程,promise,async,cla

  1. interator 遍历器
    1-1通过指针移位来遍历集合的 next() {value:0,done:true} value:指值 done:指是否遍历结束 true false
   {
    
    
        let myInterator = (args) => {
    
    
            let index = 0;
            return {
    
    
                next() {
    
    
                    return {
    
     value: args[index++], done: index < args.length ? false : true };
                }
            }
        }
        //执行该方法
        let arr = [12, 3, 4, 5, 6, 7];
        let inter = myInterator(arr);
        console.log(inter.next());//12
        console.log(inter.next());//3
        console.log(inter.next());
        console.log(inter.next());
        console.log(inter.next());
        console.log(inter.next());
        console.log(inter.next());//true

    }

1-2 es6里面规定部署interator接口遍历器
//Symbol.interator 接口部署遍历器
//部署遍历器
1-3 //obj is not iterable 默认object对象不存在遍历器
//for of能不能直接遍历部署的遍历器
1-4 //检测默认具有的 iterator
{
//Symbol.iterator
//数组默认的遍历器名称values
1-5
//对象不存在遍历器
//可以使用现成的 Array iterator 接口实现

1-6 模拟数组的遍历器 必须存在length属性 , 遍历必须是遍历器函数
字符串默认遍历器接口 iterator
for of 遍历的集合 必须存在遍历器 iterator

  1. 异步编程函数
    2-1 es5 写异步编程:匿名函数回调 事件监听 计时器
    2-2 Generator 函数标志1: 函数关键字和 名称之间 加 *
    //内部存在yield 表达式、
    //return 可以不写
   function* gen() {
    
    
            yield 1;
            yield 2;
            return 3;
        }
        // Generator 函数返回的是遍历器
        let genm = gen();
        console.log(genm.next());
        console.log(genm.next());
        console.log(genm.next());
        console.log(genm.next());

2-3 //next 参数 作为上次yield表达式的返回值
//首次next 传值无效
yield 表达式 是无返回值的 undefined
//next(参数) 传递的是上次的yield表达式的返回值

  let a = 1;
    let b = 2;
    function* genlist() {
    
    
        let c = yield a + b;
        yield c * b;
        console.log(c);
    }
    let meth = genlist();
    console.log(meth.next());
    console.log(meth.next(10));
    console.log(meth.next());
 let fun = function* () {
    
    
                for (let i = 0; true; i++) {
    
    
                    let res = yield i;
                    if (res) {
    
    
                        i = -1;
                    }
                }
            }
            let f = fun();
            console.log(f.next());//0
            console.log(f.next());//1
            console.log(f.next(true));//2

2-4 因为Generator 函数返回的是 iterator 遍历器,所以说可以for of遍历

  1. 异步解决方案
    promise, 是一个对象,里面状态 pending 进行 fulfilled 成功 rejected 失败 都不受外界影响
    ,状态一旦放生变化 不会在改
    3-1 //如何创建promise
    //Promise() 函数创建的
 {
    
    
        //简单的primise
        //Promise()  函数接收的是一个匿名函数回调
        //resolve  成功
        //reject  失败
        // 上面这两个参数  都是函数执行
        let promise = new Promise(function (resolve, reject) {
    
    
            //模拟
            let istrue = false;
            if (istrue) {
    
    
                setTimeout(function () {
    
    
                    resolve('数据');
                }, 1000);
            }
            else {
    
    
                reject('失败');
            }
        });

3-2 //用法是

 // promise.then(function (result){
    
    
        //     //成功的函数
        //     console.log(result);
        // }).catch(function (error){
    
    
        //     console.log(error);
        // });
 promise.then(function (result) {
    
    
                console.log(result);//成功!
            }, function (error) {
    
    
                console.log(error);//失败
            });
  1. //async 函数 也是异步编程的解决访问
    {
    //async 函数返回的是promise对象
    //async function前面 加async关键字
    总结:1. yeild不返回值,如果给值给的是上一次yeild里的值
    2.面试题
 let a=2;
            let b=3;
            let c=4;
            let fun2=function*(){
    
    
                yield a*(yield b+c);

            }
            let f1=fun2();
            console.log(f1.next());//7
            console.log(f1.next());//NAN
            console.log(f1.next(10));//undefinde

            //yield  表达式  是无返回值的  undefined   
            //next(参数)  传递的是上次的yield表达式的返回值
function* foo(x) {
    
    
  var y = 2 * (yield (x + 1));
  var z = yield (y / 3);
  return (x + y + z);
}

var a = foo(5);
a.next() // Object{
    
    value:6, done:false}
a.next() // Object{
    
    value:NaN, done:false}
a.next() // Object{
    
    value:NaN, done:true}

var b = foo(5);
b.next() // {
    
     value:6, done:false }
b.next(12) // {
    
     value:8, done:false }
b.next(13) // {
    
     value:42, done:true }  13+24+5
  1. //类里面的属性不着方法
    //1.静态 static
    //2.非静态
    //对象点属性和方法 全是非静态的
    //静态的属性和方法 使用类名称调用

猜你喜欢

转载自blog.csdn.net/lcywan/article/details/115040277