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 中,这个属性只读。 & m6 p1 t6 ~% ^8 [' h% p; ~* K) R
但在现代 JavaScript 中,未定义不是关键词,因此函数中的变量可以命名为未定义,并隐藏整体属性。 & l7 x6 R9 U* T+ H
如果你担心这种边缘(不太可能),你可以使用它void 获得特殊操作符undefined值本身:[code]if(myVariable === void alert("myVariable is the special value `undefined`");} , v/ z! O5 S. p% f9 F( w