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

Referenced to undefined property

22 views
Skip to first unread message

Mark Porter

unread,
Aug 25, 2007, 7:58:36 PM8/25/07
to
I am trying to use the FEATURE_WARNING_AS_ERROR flag, and in general
it is helping me avoid common errors. One thing I can't figure out is
this error:

Referenced to undefined property "times" (file:/data/tomcat/webapps/
myna/shared/js/profiler.js#107)

106: var my = arguments.callee;
107: if (!my.times) my.times = {}
108: var curLabel = element.label;

The other common variants cause the same error:
if (my.times === undefined)
if (typeof my.times === 'undefined')

I can get around this with something like this:

if (!my["times"]) my.times = {}

This seems like a hack. Is this the "proper" way to check for the
existence of an object property?

Martin Honnen

unread,
Aug 26, 2007, 7:30:26 AM8/26/07
to
Mark Porter wrote:


> This seems like a hack. Is this the "proper" way to check for the
> existence of an object property?

There is the |in| operator e.g.
if ("times" in my)

Then there is a method hasOwnProperty
if (my.hasOwnProperty("times"))

The difference is that |in| finds properties in the prototype chain
while hasOwnProperty does not.

<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Special_Operators:in_Operator>
<URL:http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Object:hasOwnProperty>


--

Martin Honnen
http://JavaScript.FAQTs.com/

Norris Boyd

unread,
Aug 26, 2007, 3:47:52 PM8/26/07
to

This is a bug in Rhino. Here's SpiderMonkey's behavior:

js> if (o.foo) print(3)
js> if (o.foo == "undefined") print(3)
typein:4: strict warning: reference to undefined property o.foo
js> if (typeof o.foo == "undefined") print(3)
3

So a reference to an undefined property in strict mode is okay as the
operand of typeof or in a conditional.

I've filed bug https://bugzilla.mozilla.org/show_bug.cgi?id=393785.

--N

Norris Boyd

unread,
Aug 31, 2007, 4:53:53 PM8/31/07
to

I just checked in changes to CVS so this is now fixed. Rhino now
suppresses warnings for undefined property o.p for the following
constructs:

typeof o.p
if (o.p)
if (!o.p)
if (o.p == undefined)
if (undefined == o.p)

--N

Mark Porter

unread,
Sep 1, 2007, 5:38:03 PM9/1/07
to
> > I've filed bughttps://bugzilla.mozilla.org/show_bug.cgi?id=393785.

>
> > --N
>
> I just checked in changes to CVS so this is now fixed. Rhino now
> suppresses warnings for undefined property o.p for the following
> constructs:
>
> typeof o.p
> if (o.p)
> if (!o.p)
> if (o.p == undefined)
> if (undefined == o.p)
>
> --N

That will be perfect. Will that be in 1.6R8 when it is released? Also,
will "if (o.p === undefined)" work as well?

I'm ashamed to admit that I'm currently suppressing this error by
using a indexOf() on the error message, and I look forward to removing
that hack.

Message has been deleted
0 new messages