Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

undefined variables

342 views
Skip to first unread message

Stuart

unread,
Aug 24, 2004, 10:39:25 PM8/24/04
to
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.

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? -OR- Would you
just not call defineProperty for a variable that you wanted to be undefined?

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?

Thanks in advance,

Stuart Broad


Igor Bukanov

unread,
Aug 25, 2004, 4:50:36 AM8/25/04
to Stuart
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

Brendan Eich

unread,
Aug 25, 2004, 10:18:33 AM8/25/04
to
Igor Bukanov wrote:
> 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.


The undefined value is the initial value of properties created without
any other value, and the value returned when you access a property by a
qualified expression (e.g., obj.prop, not prop) and no such property
exists in the object (meaning directly in the object, or indirectly in
an object on its prototype chain).

The null value is different, and connotes "no object" or "null
reference". It's occasionally useful to have a distinct value for these
connotations from the undefined value, but note that null == undefined
(and vice versa; == is reflexive and symmetric but not transitive; of
course, as you would hope, null !== undefined).

/be

0 new messages