手写new操作符

131 阅读1分钟
function myNew(fn, ...args){
    let obj = Object.create(fn.prototype);
    let res = fn.call(obj, ...args);
    if(res && (typeof res == 'object') || (typeof res == 'function'))){
        return res;
    }
    return obj;
}
let p1 = myNew(Person, "lihua", 18);
console.log(p1.name);
console.log(p1);
p1.say();

作者:Big shark@LX 链接:juejin.cn/post/700463…