跟着敲完 F12运行 网页运行报错
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>cacheStorage</title>
</head>
<body>
<script>
'use strict'
//匿名函数包裹,后面加个()表示执行
;(function(){
document.write('test');
var ls = localStorage;
function oops(){
return console.wran('your browser is not supported localStorage API')
}
function getItem(key){
var data = ls.getItem(key)||{};
data = JSON.parse(data);
if(data.time === 0){
return data.value;
}else if (Date.now > data.time){
ls.removeItem(key);
return '数据已经过期';
}else{
return typeof date.value !=='undefined'?data.value:'未找到数据';
}
}
function setItem(key,value,time){
if(typeof(key) === 'undefined'){return}
var data = {
time: time ? window.Date.now()+ time : 0,
value: value
}
data = JSON.stringify(data)
//如果数据超出localStorage的配额则清空数据
try{
ls.setItem(key,data);
}catch (e){
ls.clear();
ls.setItem(key,data);
}
}
function removeItem(key){
ls.removeItem(key);
}
function clear(){
ls.clear();
}
//给Window一个参数,并给它赋予方法
window .cacheStorage = {
getItem: ls? getItem : oops,
setItem: ls? setItem : oops,
removeItem: ls ? removeItem : oops,
clear: ls ? clear : clear
}
})()
</script>
</body>
</html>
cacheStorage.setItem('key','value',1000)
VM222:2 Uncaught ReferenceError: cacheStorage is not defined
at <anonymous>:2:1
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
at Object.InjectedScript.evaluate (<anonymous>:664:21)
正在回答 回答被采纳积分+1
- 参与学习 315 人
- 提交作业 136 份
- 解答问题 626 个
如果你有H5、CSS3、JS基础,热爱前端并希望在前端应用方面不断进步,那就来吧,本路径为你带来的课程有HTML5进阶、CSS3进阶、JS面向对象、jQ基础、Less等,助你进一步提升前端开发技能
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星