其他分享
首页 > 其他分享> > 封装的storage工具函数

封装的storage工具函数

作者:互联网

class WHCache {
	constructor(isLocal = true) {
		this.storage = isLocal ? localStorage : sessionStorage;
	}
	setItem(key, value) {
		if (value) {
			this.storage.setItem(key, JSON.stringify(value));
		}
	}
	getItem(key) {
		let value = this.storage.getItem(key);
		if (value) {
			value = JSON.parse(value);
			return value;
		}
	}
	removeItem(key) {
		this.storage.remove(key);
	}
	clear(key) {
		this.storage.clear(key);
	}
	key(index) {
		return this.storage.key(index);
	}
	length() {
		return this.storage.length;
	}
}
const local = new WHCache();
const session=new WHCache(false);


export {
local,session
}

标签:return,函数,storage,value,WHCache,key,getItem,封装
来源: https://www.cnblogs.com/wayhome123/p/15924706.html