function Father(name) {
this.name = name
this.sayWorld = function () {
console.log(`Hello ${this.name}!`)
}
}
// const fa = new Father('小明')
// fa.sayWorld()
function myNew(that, ...args) {
const obj = Object.create(null)
obj.__proto__ = that.prototype
const res = that.call(obj, ...args)
return res instanceof Object ? res : obj
}
const fa1 = myNew(Father, '小明')
fa1.sayWorld()
JS手写实现new的实现方式
最新推荐文章于 2025-03-08 10:31:01 发布