Subj : Re: Undefined Data State? To : netscape.public.mozilla.jseng From : Brian Genisio Date : Mon Jan 26 2004 07:41 am Brendan Eich wrote: > Brian Genisio wrote: > >> Hello all, >> >> Lets say in the browser, you use an object that is undefined, such as : >> >> alert(window.opener); // assuming window.opener is undefined >> >> In this case, the browser will print "undefined". >> >> My question: >> >> Can the JSObject have a value of undefined? > > > > An object is a type of value, so it can't have two values, itself and > undefined. If you look at the ECMA-262 Edition 3 spec, you can see that > the [[DefaultValue]] internal method tries toString, then valueOf. So > you could define a valueOf method on an object that returned undefined > (the undefined value). But what would be the point? > >> Or does the object's private data need to keep that state... for >> instance, is the alert method checking a valid private object? > > > > The private data slot of some native objects has nothing to do with any > of this. I'm not sure why you think it does -- is there some glaring > misstatement in the docs that needs to be fixed? > >> Or is it checking the JSObject to see if it undefined? > > > > What JSObject? For window.opener, window must denote an object, or be > of a type able to be converted to an object (boolean => Boolean, number > => Number, string => String). If window referred to null or undefined, > you would get a "window has no properties" ReferenceError on evaluating > window.opener. If window is or can be converted to an object, then if > it has a property named 'opener', that property's value will be fetched > (the prototype chain will be searched as usual). If it has no such > property (in window or any prototype), then undefined is the result of > evaluating window.opener. In this case, there is no second object. > > If window.opener denotes a property whose value is another object (the > opening window), then that second object will be the result. But > there's no overloading of object and undefined values on a single > property, in any event. > >> In other words, does the JS engine maintain undefined state? Or does >> the programmer maintain undefined state with a valid object? > > > > I still don't know what you mean. The Undefined type has one value, > undefined (also generated via void 0 and many other forms). It's not an > object. An object value can't coexist in a property with any other > value. You can make an object convert to undefined via valueOf(), but > only in certain situations: when being converted to a number, in which > case the undefined further converts to NaN; or to a string, in which > case you get "undefined": > > js> o = {valueOf:function(){return undefined;}}; > [object Object] > js> o - 2; > NaN > js> o + 'hi'; > undefinedhi > > (Per ECMA-262, an object never converts to a boolean value other than > true, so you can write 'if (window.opener)' to test for a non-null, > non-undefined, non-false, non-zero, not-empty-string reference without > having to say != null or otherwise constrain the expression to have > object or null type.) > > /be Thanks for the clarification. I am a new spidermonkey user, who got confuesed between the multiple levels of abstraction; jsvals represent JSObjects, which have private data that points to an interface to the object I am representing... it gets confusing quickly. Basically, I was writing a getProperty function for my object, and I was confused to what I needed to return in order to return undefined. As I now understand (I could still be wrong), that I dont need to set anything to the return jsval for it to be undefined... if I leave it alone and return true from the getProperty function, then undefined is returned to the javascript code by default. Thanks, Brian .