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 中,这个属性只读。. w! b/ w: P; i" ]+ L; C
但在现代 JavaScript 中,未定义不是关键词,因此函数中的变量可以命名为未定义,并隐藏整体属性。7 S- D2 c7 S- V6 z, X: M; c6 l
如果你担心这种边缘(不太可能),你可以使用它void 获得特殊操作符undefined值本身:[code]if(myVariable === void alert("myVariable is the special value `undefined`");}( F' @: _1 Y" R9 g) e/ g