Jason-linjiayu / web-blob

1 stars 0 forks source link

实现一个new #8

Open Jason-linjiayu opened 3 years ago

Jason-linjiayu commented 3 years ago

new的过程发生了什么?

Jason-linjiayu commented 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