JavaScript:undefined!== undefined? [英] JavaScript: undefined !== undefined?
问题描述
注意:根据 ECMAScript5.1,第15.1.1.3节,window.undefined是只读的。
NOTE: As per ECMAScript5.1, section 15.1.1.3, window.undefined is read-only.
- 现代浏览器实现这一点正确。例如:Safari 5.1,Firefox 7,Chrome 20等
- Undefined仍然可以改变:Chrome 14,...
- Modern browsers implement this correctly. for example: Safari 5.1, Firefox 7, Chrome 20, etc.
- Undefined is still changeable in: Chrome 14, ...
当我最近整合 Facebook Connect 与 Tersus ,我最初收到错误消息无效枚举尝试调用Facebook API函数时,值
和处理程序已存在
。
When I recently integrated Facebook Connect with Tersus, I initially received the error messages Invalid Enumeration Value
and Handler already exists
when trying to call Facebook API functions.
它已转问题的原因是
object.x === undefined
当'object'中没有属性'x'时返回false。
returning false when there is no property 'x' in 'object'.
我工作了通过在两个Facebook函数中用常规相等替换严格相等来解决问题:
I worked around the problem by replacing strict equality with regular equality in two Facebook functions:
FB.Sys.isUndefined = function(o) { return o == undefined;};
FB.Sys.containsKey = function(d, key) { return d[key] != undefined;};
这使得一切都适合我,但似乎暗示Facebook的JavaScript代码和我自己的。
This made things work for me, but seems to hint at some sort of collision between Facebook's JavaScript code and my own.
可能导致这种情况的原因是什么?
What could cause this?
提示:有充分证明 undefined == null
while undefined!== null
。这不是问题。问题是我们怎么得到 undefined!== undefined
。
Hint: It is well documented that undefined == null
while undefined !== null
. This is not the issue here. The question is how comes we get undefined !== undefined
.
推荐答案
事实证明你可以将window.undefined设置为你想要的任何东西,所以当object.x是 real <时,得到 object.x!== undefined
/ em>未定义。在我的情况下,我无意中将undefined设置为null。
It turns out that you can set window.undefined to whatever you want, and so get object.x !== undefined
when object.x is the real undefined. In my case I inadvertently set undefined to null.
最简单的方法是:
window.undefined = null;
alert(window.xyzw === undefined); // shows false
当然,这不太可能发生。在我的情况下,错误更微妙,相当于以下情况。
Of course, this is not likely to happen. In my case the bug was a little more subtle, and was equivalent to the following scenario.
var n = window.someName; // someName expected to be set but is actually undefined
window[n]=null; // I thought I was clearing the old value but was actually changing window.undefined to null
alert(window.xyzw === undefined); // shows false
这篇关于JavaScript:undefined!== undefined?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!