gpt4 book ai didi

JavaScript 未定义问题

转载 作者:行者123 更新时间:2023-12-02 19:56:09 25 4
gpt4 key购买 nike

            if(typeof(GUEST_IDS) != undefined){
GUEST_IDS = GUEST_IDS.substr(1);
GUEST_IDS = GUEST_IDS.split(",");
for(GP in GUEST_POINTS){
GUEST_ON = 0;
for(GID in GUEST_IDS){
if(GUEST_IDS[GID] == GP){
GUEST_ON = 1;
}
}
if(GUEST_ON == 0){
GUEST_POINTS[GP].setVisible(false);
}
}
}else{
for(GP in GUEST_POINTS){
GUEST_POINTS[GP].setVisible(false);
}
}

当我提醒 GUEST_IDS 时,它说未定义,所以如果 GUEST_IDS = undefined 为什么代码运行得好像 if(typeof(GUEST_IDS) != undefined){ 为 true?

最佳答案

typeof 返回指定类型的字符串。此外,typeof 不需要括号,最好使用 !== 而不是 !=:

if(typeof GUEST_IDS !== "undefined") {

其他要点:

  • 不要全部大写
  • 使用var
  • 使用普通的 for 循环来迭代数组;不是 for in 循环
  • 不要覆盖现有变量; GUEST_IDS 正在从字符串更改为数组
  • 使用===而不是==
  • 您可以使用像 var ids = GUEST_IDS.substr(1).split(","); 这样的链接

关于JavaScript 未定义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8625371/

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