Open Jason-linjiayu opened 3 years ago
function _new(fn,...rest) {
const obj = Object.create(fn.prototype)
const res = fn.apply(obj, rest)
return res instanceof Object ? res:obj
}
function Person(name) {
this.name = name
}
const p = _new(Person, "梁三喜")
console.log(p.name, p instanceof Person) // 梁三喜 true
new的过程发生了什么?