我要做一個檢測undefined值的
代碼:
data = {
highscore: 0,
localStorage: null,
footsteps: 0,
start: 0,
checkin: 0,
health: 6,
fed: 0,
birthday: Date.now(),
feedCount: 0,
pet_push_time: 0,
old_pet_mod: false, old_pet_birthday: Date.now(),
old_pet_days: -1,
old_pet_pet_push_time: 0,
old_pet_feedCount: 0,
back_HPbox: 0,
}
function undefined_test(val) {
if (val === undefined){
return 1
} else { return 0
} //1表示這個值未定義
}
undefined_test(data.test);
他不會回傳1
我把它改成這樣就好了
function undefined_test(val) {
if (val == null || val === undefined) {
return 1
} else {
return 0
}
}
我剛測試,正常呀
有回傳1呀
看這邊
https://jsfiddle.net/poopoo888888/4qstLuye/1/