闭包实现单体模式
//指定单体命名空间 const Ext = {}; //执行函数的结果返回给Ext.base Ext.base=(function () { let singleton; //初始化函数 function init() { //私有成员变量 let a1 = "a1"; let a2 = "a2"; let f1 = function () { console.log("f1"); }; let f2 = function () { console.log("f2"); } ; return { attr1: a1, attr2:a2, func1:f1, func2:f2, }; } return { getInstance: function () { if(!singleton){ //单体未实例化则实例化接化单体 singleton = init(); } return singleton; } } })(); let instance = Ext.base.getInstance(); console.log(instance.attr1); instance.func1(); instance.func2();