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 =未定义,为什么代码像if(typeof(GUEST_IDS)!= undefined){是真的那样运行?

最佳答案

typeof返回指定类型的字符串。另外,typeof不需要括号,并且在!==上使用!=是一个好习惯:

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


其他要点:


不要大写
使用var
使用普通的for循环遍历数组。不是for in循环
不要覆盖现有变量; GUEST_IDS从字符串更改为数组
使用===而不是==
您可以使用var ids = GUEST_IDS.substr(1).split(",");之类的链接

09-10 05:32
查看更多