封装的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
}
posted @ 2022-02-22 19:57  Wayhome'  阅读(50)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css