Skip to content

15. new操作符做了什么

手写

js

  function myNew(Fn) {
    if(!Fn instanceof Function) {
      throw new TypeError('Fn must be a function')
    }
    let obj = {}
    obj.__proto__ = Fn.prototype
    const nowArg = Array.prototype.slice.call(arguments, 1)
    const result = Fn.apply(obj, nowArg)
    return result ? result : obj
  }

KESHAOYE-知识星球