const singletonify = fn => {
const one = new fn()
return new Proxy(fn, {
construct(target, argumentsList, newTarget) {
return one
}
})
}
class A () {}
const SingleA = singletonify(A)
const a1 = new SingleA()
const a2 = new SingleA()
const a3 = new SingleA()
a1 === a2; //true, but why?
a2 === a3; //true
以上这段代码如何实现单例模式?为何每次创建的const one = new fn();
都相同?
慕田峪9158850
相关分类