es6标准入门教程学习(七)

19.async函数

Generator函数的语法糖(*-> async    yield->await)async 函数返回值是个Promise对象,可以用then制定下一步操作

const fs = require('fs');
const readFile = function (fileName) {
  return new Promise(function (resolve, reject) {
    fs.readFile(fileName, function(error, data) {
      if (error) return reject(error);
      resolve(data);
    });
  });
};
const gen = function* () {
  const f1 = yield readFile('/etc/fstab');
  const f2 = yield readFile('/etc/shells');
  console.log(f1.toString());
  console.log(f2.toString());
};
const asyncReadFile = async function () {
  const f1 = await readFile('/etc/fstab');
  const f2 = await readFile('/etc/shells');
  console.log(f1.toString());
  console.log(f2.toString());
};
// 函数声明
async function foo() {}
// 函数表达式
const foo = async function () {};
// 对象的方法
let obj = { async foo() {} };
obj.foo().then(...)
// Class 的方法
class Storage {
  constructor() {
    this.cachePromise = caches.open('avatars');
  }
  async getAvatar(name) {
    const cache = await this.cachePromise;
    return cache.match(`/avatars/${name}.jpg`);
  }
}
const storage = new Storage();
storage.getAvatar('jake').then(…);
// 箭头函数
const foo = async () => {};

任何一个await语句后面的Promise对象变为rejected状态,那么整个async函数都会中断执行

把await放在try...catch中就不用管操作是否成功,都会执行之后的异步操作

或者await Promise.reject(xxx).catch(xxxxx)这样子捕获错误

2.错误处理  如果await后面的异步操作出错,那么等同于async函数返回的Promise对象reject

 trycatch 放入循环中一直尝试(可)

多个await后面的操作如果不顺序相关,可以

let [foo1,foo2] = await (Promise.all([getFoo(),getBar()]));

async function dbFuc(db) {
  let docs = [{}, {}, {}];
  for (let doc of docs) {
    await db.post(doc);
  }
}

async function dbFuc(db) {
  let docs = [{}, {}, {}];
  await docs.reduce(async (_, doc) => {
    await _;
    await db.post(doc);
  }, undefined);
}

async 函数可以保留运行堆栈

const a = () => {
  b().then(() => c());
};

const a = async () => {
  await b();
  c();
};

顶层await(xx)

20.Class的基本语法

class Point{
    constructor(x,y){
        this.x=x;
        this.y=y;
    }
    toString(){
        console.log('123');
        return this.x + this.y
    }
}

2.静态方法static    类相当于是实例的原型,所有类中定义方法都会被实例继承,但是方法前面加上static关键字,那么不会被继承,而是直接通过类来调用, 父类的静态方法能被子类继承,静态方法也可以super调用,

class Foo {
  static classMethod() {
    return 'hello';
  }
}
class Bar extends Foo {
  static classMethod() {
    return super.classMethod() + ', too';
  }
}
Bar.classMethod() // "hello, too"

3.实例属性的新写法(提前罢了)不写在con实例对象this上,就和方法同一层级

4.静态属性  写在实例属性前面,加上statc关键字

5.私有方法和私有属性

私有属性/方法:在属性名之前+# 也可以设置(get set)

6.new.target属性

new 是从构造函数生成实例的命令,new.target用来返回new命令卓雍遇到构造函数,如果构造函数不是通过new Reflect.construct()调用就会返回undefined

子类继承父类时,new会返回子类

可以写出只能继承不能实例化的类

21.Class的继承

1.class可以使用extends实现继承

super关键字:表示父类的构造函数,用来新建父类的this对象

es5继承是先创造子类的实例对象this,再将父类方法添加到this

es6是将父类属性方法加到this上,再用子类构造器修改this

在子类的构造函数中,只有调用super之后,才可以使用this关键字,否则会报错,因为子类实例的构建,基于父类实例,只有super方法能调用父类实例

2.Object.getPrototypeOf()  从子类获取父类

3.super(懵了)

super作为对象时,指向父类原型对象(静态方法中指向父类)

super作为函数时,是父类构造函数

class A {}
A.prototype.x = 2;
class B extends A {
  constructor() {
    super();
    console.log(super.x) // 2
  }
}
let b = new B();

在子类普通方法中,通过super调用父类方法中,方法内部this指向子类实例

class A {
  constructor() {
    this.x = 1;
  }
  print() {
    console.log(this.x);
  }
}
class B extends A {
  constructor() {
    super();
    this.x = 2;
  }
  m() {
    super.print();
  }
}
let b = new B();
b.m() // 2

3.prototypr __proto__

4.原生构造函数的继承

5.Mixin模式的实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值