编辑代码

console.log("Hello  world!    - typescript.jsrun.net")
function customNew<T>(constructor_: Function, ...args: any[]): T {
  // 1. 创建一个空对象,继承 contructor 的原型
  const obj = Object.create(constructor_.prototype)
  // 2. 将 obj 作为 this, 执行 constructor, 传入参数
  constructor_.apply(obj, args)
  // 3. 返回 obj
  return obj
}

class Foo {
  // 属性
  name: string
  city: string = '北京'
  n: number

  constructor(name: string, n: number ) {
    this.name = name
    this.n = n
  }

  getName() {
    return this.name
  }
  
}

function foo_(name: string, n: number) {
    this.name = name
    this.n = n
}
foo_.prototype.getName = function () {
    return this.name
}

console.info(customNew(foo_, '数月', 10))