Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  148] [ 6]  / answers: 1 / hits: 118148  / 15 Years ago, wed, april 22, 2009, 12:00:00

NOTE: As per ECMAScript5.1, section 15.1.1.3, window.undefined is read-only.




  • Modern browsers implement this correctly. for example: Safari 5.1, Firefox 7, Chrome 20, etc.

  • Undefined is still changeable in: Chrome 14, ...



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.



It turned out that the cause of the problem was



object.x === undefined


returning false when there is no property 'x' in 'object'.



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;};


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?



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.


More From » javascript

 Answers
16

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.



The easiest way to see this happen is:



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

[#99669] Thursday, April 16, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;