为什么{highscorelist}没有值?


const HIGHSCOREKEY = "highscore";
highscorelist = localStorage.getItem(HIGHSCOREKEY);
if(highscorelist){
highscorelist = {
one: {navn: "AAA", score: 0},
two: {navn: "AAA", score: 0},
three: {navn: "AAA", score: 0}
};
localStorage.setItem(HIGHSCOREKEY, JSON.stringify(highscorelist));
};

这是它的代码,当我尝试执行"hilicorelist.one.score"它显示:"未捕获的类型错误:无法读取属性'one' of null">

localStorage.setItem(HIGHSCOREKEY, JSON.stringify(highscorelist));相当于localStorage.setItem(HIGHSCOREKEY, highscorelist);, localStorage将以字符串的形式存储。

所以在检索数据时执行

highscorelist = JSON.parse(localStorage.getItem(HIGHSCOREKEY));

相关内容

  • 没有找到相关文章

最新更新