我有这个对象/数组的东西:

var states = {};

states["CA"] = new State("CA", "California");
states["AR"] = new State("AR", "Arizona");
....


如何检查是否已设置states["AL"]?以下功能(在所有浏览器中)都能工作吗?

if (states["AL"] == undefined)
   alert("Invalid state");

最佳答案

推荐的:

if (typeof states["AL"] == 'undefined')
    alert("Invalid state");


Litteral undefined是可能的,并且在大多数情况下都可以使用,但是在某些浏览器(IE mac,也许其他)中不起作用。

10-06 15:38
查看更多