localStorage util

// localStorage util
var db ={
	set : function(key, obj){
		localStorage.setItem(key, JSON.stringify(obj));
	},

	get : function(key){
		return JSON.parse(localStorage.getItem(key));
	},

	each : function(fun){
		try{
			for (var i=0; i<localStorage.length; i++){
				var k = localStorage.key(i);
				fun(k,db.get(k));
			}
		} catch(e) {
			for (var key in localStorage){
				if(key === 'key') continue;
				fun(key,db.get(key));
			}
		}
	}
};

  

posted @ 2018-06-26 15:52  zhanghui_ming  阅读(448)  评论(0编辑  收藏  举报