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?
> 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/
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
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.