1.0版本
function _new(Fn,...args){
//1.创建一个空对象
let obj={}
//2.让对象的__proto__指向函数的prototype
obj.__proto__=Fn.prototype
//3.改变this指向
Fn.apply(obj,args)
//4.返回对象
return obj
}
调用:let ls=_new(Person,"lisi")
2.0版本
function _new(Fn,...args){
//若Fn不是函数
if(!Fn.hasOwnProperty("prototype")){
throw new TypeError(`Fn is not a constructor`)
}
//1.2.
let obj=Object.create(Fn.prototype)
//3.
let result = Fn.apply(obj,args)
//若函数的返回值是对象或函数则应返回这个函数或对象(new本身的用法)
if(result !== null && (typeof result=="object" || typeof result=="function")){
return result
}
//4.
return obj
}