ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的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) } } ~~~