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(",");
之类的链接