// 实现new运算符
function myNew(Fn, ...args) {
const obj = Object.create(Fn.prototype)
const result = Fn.apply(obj, args)
return result instanceof Object ? result : obj
}
function Person(name) {
this.name = name;
}
const person = myNew(Person, '张三')
console.log(person) // {name:'张三'}
javascript-实现new运算符
最新推荐文章于 2024-08-30 20:29:15 发布