js - 继承

ru shui 2021-09-14 Javascript
  • Javascript
Less than 1 minute

# new

使用 ES6 实现基本的 new 功能。

function newObject(constructor, ...args) {
  function F() {}
  F.prototype = constructor.prototype
  const obj = new F()
  const result = constructor.apply(obj, args)
  return typeof result === 'object' && result !== null ? result : obj
}
1
2
3
4
5
6
7
Last update: September 25, 2021 10:42
Contributors: Laishuxin