很容易......你可以做到這一點對2種方式
var YouTube = window["EpicKris"] ;// or this["EpicKris"] or objectContainer["EpicKris"]
if(YouTube) { //if is null or undefined (Zero and Empty String too), will be converted to false
console.log(YouTube);// exists
}else{
consol.log(YouTube);// null, undefined, 0, "" or false
}
,或者你可以
var YouTube = window["EpicKris"] ;// or this["EpicKris"] or objectContainer["EpicKris"]
if(typeof YouTube == "undefined" || YouTube == null) { //complete test
console.log(YouTube);//exists
}else{
console.log(YouTube);//not exists
}
[在JavaScript中檢測未定義的對象屬性]的可能重複(http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property-in-java腳本) – 2014-04-01 03:44:32