Not sure about effecient (have some tests at bottom), but the "correct" way 
is to use:

// notice 3 equals
if ( o.prop === undefined)
{
    // does not have prop
}
else
{
    // DOES have prop
}

null is a valid value, whereas undefined means the property doesn't exist.

hasOwnProperty doesn't look up the prototype chain (aka inheritance chain), 
so it only checks the current object.

Testing in Flash 8, o.prop === undefined averages 2 milliseconds in a 
function (since local registers are optimized in 7) whereas 
o.hasOwnProperty("name") averages 4.

----- Original Message ----- 
From: "sbyrne_dorado" <[EMAIL PROTECTED]>
To: <flexcoders@yahoogroups.com>
Sent: Thursday, December 08, 2005 7:44 PM
Subject: [flexcoders] Best Practices: determining if a key is in an Object?


I'd like to find out what the best (most efficient) way to determine
if a given key is present in an object.

obj[key] will answer "undefined" if either there is no key in object,
OR if it's value is in fact undefined.

I can of course to the potentially expensive for..in and break out if
I find the given key I am looking for, but that's not a good solution
in general.

Any hints?  Surely this has to be something AS supports...(?)

Steve







--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
Yahoo! Groups Links








------------------------ Yahoo! Groups Sponsor --------------------~--> 
Most low income households are not online. Help bridge the digital divide today!
http://us.click.yahoo.com/I258zB/QnQLAA/TtwFAA/nhFolB/TM
--------------------------------------------------------------------~-> 

--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to