Subj : Re: undefined variables To : Stuart From : Igor Bukanov Date : Wed Aug 25 2004 11:50 am Stuart wrote: > Hi, > > I am a little confused as to the use of undefined in javascript and Rhino. > > I think my fundamental problem is that I do not understand the purpose of > having both an Undefined and Null type/value in ECMAScript. I suggest to post a generic question about that topic to the newsgroup so perhaps SpiderMonkey folks could tell the story. > > If I was creating a variable: > > String name = null; > ScriptableObject.defineProperty(myScope, "userName", name, > ScriptableObject.READONLY); > > Is it possible to set the 'value' of userName to undefined? Sure, just use Context.getUndefinedValue() in place of the name. And then you may want to replace ScriptableObject.READONLY by 0: I guess you do not want read-only undefined value. BTW, when the script executes, it initialize all var names initially with: ScriptableObject.defineProperty(scope, name, Context.getUndefinedValue(), ScriptableObject.PERMANENT); where ScriptableObject.PERMANENT indicates that the property can not be deleted so "delete varName" would do nothing. > > Also can you programatically test to see if the value of a variable is > undefined? I noticed that the getProperty can return Scriptable.NOT_FOUND - > is this equivalent to undefined? NO! Scriptable.NOT_FOUND is just a tag to indicate that there is no property in the object with the given name. It has nothing to do with undefined value. If the property exist and contain undefined value you will get Context.getUndefinedValue() as the result. And if it exists and contains null in JS sense you will get Java null. Regards, Igor .