Skip to content

模拟实现 new

js
export default function myNew(constructor, ...args) {
  const obj = {}
  obj.__proto__ = constructor.prototype
  const result = constructor.call(obj, ...args)
  return typeof result === 'object' ? result : obj
}