检测未定义对象属性检查JavaScript中的对象属性是否未定义的最佳方法是什么?
4 回答
青春有我
TA贡献1784条经验 获得超8个赞
if (typeof something === "undefined") { alert("something is undefined");}
if (typeof my_obj.someproperties === "undefined"){ console.log('the property is not available...'); // print into console}
浮云间
TA贡献1829条经验 获得超4个赞
正确码
if (typeof myVar === "undefined")
myVar
退化代码。不要用。
var undefined = false; // Shockingly, this is completely legal!if (myVar === undefined) { alert("You have been misled. Run away!");}
myVar === undefined
添加回答
举报
0/150
提交
取消