封装的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
}
上一篇:Spring 的两个配置容器的讲解


下一篇:idea使用maven-assembly-plugin生成jar包的pom配置