Skip to content

其他

注意:以下方法相较原生语法既没有改进、也没有更方便,所以真的就是——看看就好。

New

mdx
import { New } from 'sth';

function Person(name: string, age: number) {
  this.name = name;
  this.age = age;
  this.introduce = function (): string {
    return 'My name is ' + this.name + ', ' + this.age + ' years old.';
  };
}

const person: any = New(Person, 'Li Lei', 18);
person.name ; // 'Li Lei'
person.age; //18
person.introduce(); // 'My name is Li Lei, 18 years old.'

function PlainObject(a: any) {
  this.b = 2;
  return {
    a,
  };
}

const object: any = New(PlainObject, 1);
object.a; // 1

function IntNumber(a: number) {
  this.b = 2;
  return a;
}

const number = new IntNumber(1);
number; // { b: 2 }