// 实现Storage,使得该对象为单例,基于 localStorage 进行封装。实现方法 setItem(key,value) 和 getItem(key)
/*
* 静态方法
*/
class Storage {
staic getInstance () {
if (!Storage.instance) {
Storage.instance = new Storage()
}
return Storage.instance
}
getItem (key) {
return localStorage.getItem(key)
}
setItem (key, value) {
return localStorage.setItem(key, value)
}
}
/*
* 闭包版
*/
function StorageBase () {}
StorageBase.prototype.getItem = function (key) {
return localStorage.getItem(key)
}
StorageBase.prototype.setItem = function (key, value) {
return localStorage.setItem(key, value)
}
const Storage = (function () {
let instance = null
return function () {
if (!instance) {
instance = new StorageBase()
}
return instance
}
})()
console