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();都相同?
添加回答
举报
0/150
提交
取消