手写一个方法实现new功能

223 阅读1分钟

new做了哪些事情

  1. 执行构造函数
  2. 当函数返回值类型为对象时,则返回该对象
  3. 当函数返回值类型不为对象时,返回该构造函数的实例化对象

开始手写代码

function _new(fn,...rest){
  //基于fn的prototype构建对象的原型
  const thisObj = Object.create(fn.prototype);
  //将thisObj作为fn的this,继承其属性,并获取返回结果为result
  const result = fn.apply(thisObj,rest);
  //根据result对象的类型决定返回结果
  return typeof result === "object" ? result : thisObj;
}

测试代码是否正确

function Person(name,age){
  this.name = name;
  this.age = age;
}
const person1 = new Person("Lee",21);
const person2 = _new(Person,"Lee",21)

两个对象效果完全一样,完美实现new