new 操作符实现原理

113 阅读1分钟

// new 操作符function _new (Fn, ...args) {  const obj = Object.create(Fn.prototype)  const result = Fn.call(obj, ...args)  return result !== null && typeof result === 'object' || typeof result === 'function' ? result : obj}

 

注:instanceof 检测不出 Object.create(null) ,因为这个值是个对象,却没有原型链,所以这里不用