0.0.1 • Published 10 months ago

esleon v0.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

esleon

esleon是一个Leon化的ES6语法的实现包。

仅供学习使用,无法用于生产

特性

Promise => Promiseon

Promiseon是一个实现了Promise/A+规范的类。支持Promise的异步微任务、链式调用、resolve、reject、all等功能。

new Promiseon((resolve) => {
  resolve(1);
})
  .then((value) => {
    console.log("获取结果", value);
    return Promiseon.resolve(2);
  })
  .finally(() => {
    console.log("结束");
  });

async/await => asynceon

asynceon实现了类似于async/await的效果。不过由于没有语法层面的await,所以改为了yield。使用asynceon包装函数,和ES6使用async修饰函数一样。函数体内部异步转同步,最终返回的是一个Promiseon。

// ES6的async语法
const asyncFunction = async (base: number) => {
  const a = await asyncGetA();
  const b = await asyncGetB();
  return base + a + b;
}
// asynceon语法
const asynceonFunction = asynceon(function* (base: number) {
  const a = yield asyncGetA();
  const b = yield asyncGetB();
  return base + a + b;
});

Class => Classeon

ES6的Class也是需要语法支持的,比如classsupernewextendsstatic这些关键字。我们自己去实现的话,只能通过函数去处理。

最终实现的功能有new对象实例静态方法调父类构造函数成员方法和静态方法继承等。

// ES6的class语法
class Person {
  name: string;

  constructor(name: string) {
    this.name = name;
  }

  greet() {
    return `Hello, my name is ${this.name}`;
  }

  static run() {
    return "run";
  }
}

class SuperMan extends Person {
  power: string;

  constructor(name: string, power: string) {
    super(name);
    this.power = power;
  }

  shoot() {
    return `I can shoot ${this.power}`;
  }

  static fly() {
    return "fly";
  }
}

// defClasseon/newClasseon语法
const PersonClasseon = defClasseon(
  function (name: string) {
    this.name = name;
  },
  null,
  {
    greet() {
      return `Hello, my name is ${this.name}`;
    },
  },
  {
    run() {
      return "run";
    },
  }
);

const SuperManClasseon = defClasseon(
  function (name: string, power: string) {
    this.super(name);
    this.power = power;
  },
  PersonClasseon,
  {
    shoot() {
      return `I can shoot ${this.power}`;
    },
  },
  {
    fly() {
      return "fly";
    },
  }
);

文档和仓库

源码地址:https://github.com/missmess/leon-build-wheel/tree/main/packages/reacteon

0.0.1

10 months ago