废话不多说直接上代码
最核心的是new需要传入
(constructor构造函数,参数)
function myNew(constructor, ...args){
//首先创建一个构造函数的原型对象
const obj = Object.create(constructor.prototype);
//对obj对象第一次触发一次构造函数,并且保存结果到result
const result = constructor.apply(obj,args);
//返回的时候需要判断构造函数返回的是否是一个Object,不是的话就返回obj
return (typeof result === 'object' && result !== null) : result : obj
}