gpt4 book ai didi

javascript - 如果未定义,如何将本地存储设置为 0?

转载 作者:行者123 更新时间:2023-11-30 07:39:00 25 4
gpt4 key购买 nike

if localStorage["BestScore"] = undefined;
{
localStorage["BestScore"]=0;
maxScore=0;
}

var maxScore=localStorage["BestScore"];
var newScore=false
function drawScore(score) {
if (newScore == true && score < maxScore) {
newScore = false;
}
if (score > maxScore) {
newScore = true;
localStorage["BestScore"] = score;
if ([5, 10, 15, 20].indexOf(score) !== -1) {
play(sndMedal);
} else {
play(sndGain);
}
}

This code is to set the max score and then store it but it doesn't seem to set the local storage to 0 if undefined.

最佳答案

if localStorage["BestScore"] = undefined;

应该是:

if( typeof localStorage["BestScore"] === 'undefined' )

关于javascript - 如果未定义,如何将本地存储设置为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22452844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com