if(o.myProperty === undefined) { alert("myProperty value is the special value `undefined`");} K1 |2 Y @9 p0 r9 C+ L; {4 B8 }" c
检查对象是否实际上没有这样的属性,因此undefined当您尝试访问它时,默认返回: & X. @( S1 O9 l9 H5 `! H- B( N A( C
if(!o.hasOwnProperty('myProperty) { alert("myProperty does not exist");}' f) H" x- m7 i" ?) m
检查与标识符相关的值是否为特殊值undefined,或者标识符是否尚未声明。注意:这种方法是引用未声明(注:与 值不同undefined)唯一没有早期错误的方法:5 |# _, E; r( n3 N; n
3 _0 o4 Z" y8 ~, r* r) o1 n' n
if(typeof myVariable === 'undefined') { alert('myVariable is either the special value `undefined`,or it has not been declared」code]在 ECMAScript 5 以前的 JavaScript 版本中,全局对象被称为undefined属性是可写的,所以foo === undefined如果不小心重新定义了一个简单的检查,可能会发生意外。JavaScript 中,这个属性只读。) q4 l6 M4 B- K! \ f
如果你担心这种边缘(不太可能),你可以使用它void 获得特殊操作符undefined值本身:[code]if(myVariable === void alert("myVariable is the special value `undefined`");} ) }( E/ J; w/ M6 }: Z5 ~6 p