单例模式

65 阅读1分钟
function SingleWrapper(cons) {
  if (!(cons instanceof Function) || !cons.prototype) {
    throw new Error('不是合法的构造函数')
  }
  let instance
  return function (...args) {
    if (!instance) {
        instance = new cons(...args)
    }
    return instance
  }
}