You're right, I wasn't thinking about that fact that any dynamic class
like Object or XML will compile dot references to any property.
My coding conventions are that it's OK to write o.foo (where o is a
dynamic object) if I'm sure that foo exists on o. But if it might not
and I want to test whether it is there, I write if ("foo" in o).
- Gordon
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Troy Gilbert
Sent: Friday, May 25, 2007 9:30 AM
To: [email protected]
Subject: Re: [flexcoders] Checking if an Object has as property/element
#2 isn't always true, for example in the case of XML. Here's the idiom I
use for pulling elements out of an XML document:
if (xml.myTag) myValue = xml.myTag;
Is that a decent idiom? Also, if the return type is Object, won't the
compiler allow you to access any property with '.' notation? For
example, I use Objects for key/value pairs all over my code using code
like Number( myObject.myProperty) where "myProperty" is most certainly
not known at compile time (it's dynamically generated through several
API's).
Troy.
On 5/24/07, Gordon Smith <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> >
wrote:
I'd argue that best-practice is #4, the 'in' operator.
#1 is Ecmascript legacy useful for prototype-based inheritence,
to tell whether an object has the property or one farther up its
prototype chain. Flex makes almost no use of prototype-based
inheritence.
#2 isn't useful because if it compiles, the property definitely
exists.
#3 can be misleading because you can't distinguish between the
property not existing and the property containg a value which coerces to
false.
- Gordon
________________________________
From: [email protected]
[mailto:[email protected] <http://yahoogroups.com> ] On Behalf
Of Troy Gilbert
Sent: Thursday, May 24, 2007 11:19 AM
To: [email protected]
Subject: Re: [flexcoders] Checking if an Object has as
property/element
Interesting, I've never seen that one, Alex... nicely
expressive.
So, in summary, one can test the existent of a property with:
if (myObject.hasOwnProperty("property")) ...
if (myObject.property ) ...
if (myObject["property"]) ...
if ("property" in myObject) ...
The first one is a function returning a Boolean. Obvious.
The second and third ones return the actual values, which
implicitly convert to true (for everything but null and "", which
implicitly convert to false), or if the property doesn't exist returns
undefined, which implicitly converts to false.
The fourth one... well, that's basically identical to the first
one where it returns a Boolean.
Troy.
On 5/24/07, Alex Harui < [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote:
if ("blah" in Object)
Should also work
-----Original Message-----
From: [email protected]
<mailto:flexcoders%40yahoogroups.com> [mailto:
[email protected] <mailto:flexcoders%40yahoogroups.com> ] On
Behalf Of Tom Chiverton
Sent: Thursday, May 24, 2007 6:53 AM
To: [email protected]
<mailto:flexcoders%40yahoogroups.com>
Subject: Re: [flexcoders] Checking if an Object has as
property/element
On Thursday 24 May 2007, Christopher Olsen wrote:
> If i do if Object.blah == null i get an error because
blah doesn't
exist
hasOwnProperty(), or just if (Object.blah)
--
Tom Chiverton
Helping to globally maintain error-free solutions
on: http://thefalken.livejournal.com
<http://thefalken.livejournal.com>
****************************************************
This email is sent for and on behalf of Halliwells LLP.
Halliwells LLP is a limited liability partnership
registered in England
and Wales under registered number OC307980 whose
registered office
address is at St James's Court Brown Street Manchester
M2 2JF. A list
of members is available for inspection at the registered
office. Any
reference to a partner in relation to Halliwells LLP
means a member of
Halliwells LLP. Regulated by the Law Society.
CONFIDENTIALITY
This email is intended only for the use of the addressee
named above and
may be confidential or legally privileged. If you are
not the addressee
you must not read it and must not use any information
contained in nor
copy it nor inform any person other than Halliwells LLP
or the addressee
of its existence or contents. If you have received this
email in error
please delete it and notify Halliwells LLP IT Department
on 0870 365
8008.
For more information about Halliwells LLP visit
www.halliwells.com <http://www.halliwells.com> .
--
Flexcoders Mailing List
FAQ:
http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
<http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt>
Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.com
<http://www.mail-archive.com/flexcoders%40yahoogroups.com>
Yahoo! Groups Links