Fork me on github

【JS】实现new操作符

https://github.com/zjy4fun/notes/tree/main/demos/js-new

 

const myNew = (constructorFn, ...args) => {
    const obj = Object.create(constructorFn.prototype)
    const result = constructorFn.apply(obj, args)
    if(typeof result === 'object' && result !== null) {
        return result
    }
    return obj
}

 

posted @ 2023-09-10 20:41  zjy4fun  阅读(11)  评论(0编辑  收藏  举报