localStorage工具类的封装分享

在实际项目开发的过程中,我们难免会用到本地缓存这些东西,像cookie,localStorage,sessionStorage啊,虽然这些很简单,今天分享一个基于localStorage工具类的封装。

/**
 * localStorage 帮助类
 * 小提示: 用static修饰的方法,可以用类名加方法名直接调用
 */
class localStorageUtil {
     
 /**
  * localStorage前缀(每个公司localStorage都会存自己特有的标识,类似于项目名称缩写,或公司拼音名称缩写)
  * @return {[type]} [description]
  */
 static _key = (key) => {
     
     return 'ym' + key;
 };

 /**
  * 根据key获取localStorage
  * @param  {[type]} key [description]
  * @return {[type]}     [description]
  */
 static get = (key) => {
     
     return localStorage.getItem(localStorageUtil._key(key));
 };

 /**
  * 设置localStorage
  * @param  {[type]} key [description]
  * @param  {[type]} val [description]
  * @return {[type]}     [description]
  */
 static set = (key, val) => {
     
     return localStorage.setItem(localStorageUtil._key(key), val);
 };

 /**
  * 删除指定的localStorage
  * @param  {[type]} key [description]
  * @return {[type]}     [description]
  */
 static del = (key) => {
     
     return localStorage.removeItem(localStorageUtil._key(key));
 };

 /**
  * 清空localStorage
  * @return {[type]} [description]
  */
 static clear = () => {
     
     return localStorage.clear();
 };
}
export default localStorageUtil;

如何错误请指正!!!

你可能感兴趣的:(javascript,es6)