/*
// 如下是initStorageEvent()方法参数,其中两个参数我默认写成false了,一般都不用改动
initStorageEvent(eventType, false, false, keyArg, oldValueArg, newValueArg, urlArg, storageAreaArg);
*/
var myStorage = {
setItem : function(k,v){
// 此处可以理解为在原来的方法上添加了监听,此处的
var evn = document.createEvent("StorageEvent");
evn.initStorageEvent('logSetItemEvent', false, false, k, localStorage.getItem(k), v, "", "");
window.dispatchEvent(evn);
localStorage.setItem(k,v);
},
removeItem : function(k){
var evn = document.createEvent("StorageEvent");
evn.initStorageEvent('logRemoveItemEvent', false, false, k, localStorage.getItem(k), null, "", "");
window.dispatchEvent(evn);
localStorage.removeItem(k);
}
};
// 此处的e就是上面var的evn,开始给window绑定方法
window.addEventListener("logSetItemEvent",function(e){
console.log(e);
},false);
window.addEventListener("logRemoveItemEvent",function(){
console.log("xxxxxxxxxx");
},false);
myStorage.setItem("name","LiuYashion")
myStorage.removeItem("name")