每天一道手写题
手写new核心代码
function myNew(fn, ...arg) {
let obj1 = {}
if (fn.prototype)
obj1.__proto__ = fn.prototype
let res = fn.apply(obj1, arg)
if (res && (typeof res == 'function' || typeof res == 'object'))
return res
return obj1
}
测试
function myNew(fn, ...arg) {
let obj1 = {}
if (fn.prototype)
obj1.__proto__ = fn.prototype
let res = fn.apply(obj1, arg)
if (res && (typeof res == 'function' || typeof res == 'object'))
return res
return obj1
}
function a(name, age, height) {
this.name = name
this.age = age
this