赞
踩
设计模式:针对特定问题提出的简洁优化的解决方案
// 利用闭包函数封装
let singleton = (function() {
// 工具类
class Utils { }
let tool;
return function () {
// 每次使用都判断tool中是否已存在实例对象
if (!tool) tool = new Utils();
return tool;
}
})();
let t1 = singleton();
let t2 = singleton();
console.log(t1 === t2); // true
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。