ES6中的async和Generator,class类继承

{

  async function method() {
            try {
                let aw1=await new Promise(function (resolve,reject){
                    //promise  内部返回reject
                    if(true)
                    {
                        setTimeout(resolve,1000,"数据")
                    }
                    else{
                        reject("失败");
                    }
                });
                let aw2=await (function (params){
                    return new Promise(function (resolve,reject){
                        setTimeout(function (){
                            resolve("获取"+params);
                        },1000)
                    });
                })(aw1);
                return aw2;
            }
            catch (e) {
               throw new Error(e);
            }
            finally{
                console.log("抓到异常最后的处理代码");
            }
        }
        method().then(function (res){
            console.log(res);
        }).catch(function (err){
            console.log(err);
        }).finally(function (){
            console.log("最终执行的");
        });
    }

function * method(){
yield "数据1";
yield "数据2";
yield "数据3"
}
let generatorfun=method();//执行该函数的时候 函数并不执行 只是返回了该函数的状态
console.log(generatorfun);
//执行 调用便利器方法next 移动方法内部的指针
//next 返回一个对象 value 指yield表达式的值 done false 指遍历还没有结束
console.log(generatorfun.next());
console.log(generatorfun.next());
console.log(generatorfun.next());
console.log(generatorfun.next());//{value: undefined, done: true} 遍历结束了

/*

  • 之前js 通过修改原型链 实现继承
  • es6里面通过extends 实现继承
  • 相比 比之前更清晰 方便
  • */
    //父
    class Person{
    name="";
    sex="";
    age="";
    static clothes="校服";
    constructor(name,sex,age){
    this.name=name;
    this.sex=sex;
    this.age=age;
    }
    toString(){
    console.log(this.name+"是学生");
    }
    }
    //子
    //子类继承父类之后继承父类的所有的属性和方法
    class Student extends Person{
    constructor(name,sex,age){
    //super 函数使用方式
    super(name,sex,age);//super 指向父类的构造
    //super 返回的是子类的实例
    //Person.prototype.constructor.call(this);

        //super  当做对象使用 指向的是父类的原型对象   在静态方法当中指向父类
    
        //super当对象使用
        super.toString();
    }
    //静态方法
    static getClothes(){
        //  这里的super  指向的是父类
        console.log(super.clothes);
    }
    toJob(){
    
    }

    }

    //实例化学生
    let stu=new Student("张三","男",28);
    console.log(stu);
    Student.getClothes();
    //
    console.log(Student.clothes);

    console.log(stu.proto);//子类的proto属性,表示构造函数的继承,总是指向父类。
    console.log(Student.prototype.proto);//指向的是父类的原型对象

猜你喜欢

转载自blog.51cto.com/14584021/2480151
今日推荐