企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
# 单例模式 ES5 ~~~ function Storage (){} Storage.getInstance = (function () { var instance = null return function () { if (!instance) { instance = new Storage() } return instance } }()) Storage.prototype.getItem = function (name) { return localStorage.getItem(name) } Storage.prototype.setItem = function (name, value) { return localStorage.setItem(name, value) } ~~~ <br> ES6 ~~~ class Singleton { constructor () { this.instance = null } static getInstance () { if (!this.instance) { this.instance = new Singleton() } return this.instance } getItem (name) { return localStorage.getItem(name) } setItem (name, value) { return localStorage.setItem(name, value) } } ~~~