FWIW you can test for a property using "in"
 
if (getChildren in obj)

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Mike Morearty
Sent: Tuesday, April 03, 2007 5:44 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: getChildren



Gordon's way does work -- and in fact it can be taken even further by
not only checking that there is a property called "getChildren", but
also that that property is a function:

if (obj.hasOwnProperty("getChildren") && 
typeof obj["getChildren"] == "function")
{
children = obj.getChildren();
}

However, I think Dordea's answer -- to test "obj is Container" -- is
actually what you should be using. You are not really trying to check
whether obj has a getChildren() function -- you are trying to check
whether, semantically speaking, it is a container.

To take the old object-oriented programming example, let's say there
was a class called Artist, with a function draw(). If you just tested
for the existence of function "draw" and then called it, that doesn't
necessarily mean you are asking an Artist to draw -- that just means
you have asked *something* to draw. Maybe Cowboy also has a draw()
function, to draw his gun. Oops!

So back to your example, I think probably what you're really trying to
say is, "If the object that was passed in is a Container, then I want
to get its children." So, "if (obj is Container)" is the way to go.

- Mike Morearty, Flex Builder

--- In flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
, "Gordon Smith" <[EMAIL PROTECTED]> wrote:
>
> I think this may be another way...
> 
> if (obj.hasOwnProperty("getChildren"))
> children = obj.getChildren();
> 
> - Gordon
> 
> ________________________________
> 
> From: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
[mailto:flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com>
] On
> Behalf Of dordea cosmin
> Sent: Tuesday, April 03, 2007 12:13 PM
> To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> 
> Subject: Re: [flexcoders] getChildren
> 
> 
> 
> only objects that inherit the Container class have this functions.
When
> i have a situation like yours, I usually do something like :
> if ( obj is Container)
> {
> //code here
> }
> 
> and it works fine. It is possible not to work if your object
> was originally an Object , and you further created your 
> object using a class reference.
> 
> 
> ----- Original Message ----
> From: Michael Schmalle <[EMAIL PROTECTED]>
> To: flexcoders@yahoogroups.com <mailto:flexcoders%40yahoogroups.com> 
> Sent: Tuesday, April 3, 2007 11:51:48 AM
> Subject: Re: [flexcoders] getChildren
> 
> 
> 
> Hi,
> 
> You could do this;
> 
> try {
> children = obj.getChildren( );
> }
> catch (e:Error)
> {
> trace(e, "obj did not have getChildren( )");
> }
> 
> 
> Peace, Mike
> 
> 
> 
> 
> On 03 Apr 2007 11:41:25 -0700, blc187 <[EMAIL PROTECTED] com
> <mailto:[EMAIL PROTECTED]> > wrote: 
> 
> is there a way to tell if an object has a getChildren method
> before 
> calling it?
> 
> I'm calling something that takes any type as the first argument
> public function myFunc(obj:* ):void
> 
> I need to tell if obj has a getChildren( ) method before I call
> it and 
> get an error saying Error #1006: getChildren is not a function.
> 
> 
> 
> 
> 
> 
> 
> 
> -- 
> Teoti Graphix
> http://www.teotigra phix.com <http://www.teotigraphix.com
<http://www.teotigraphix.com> > 
> 
> Blog - Flex2Components
> http://www.flex2com ponents.com <http://www.flex2components.com
<http://www.flex2components.com> > 
> 
> You can find more by solving the problem then by 'asking the
question'. 
> 
> 
> ________________________________
> 
> Get your own web address.
>
<http://us.rd.yahoo.com/evt=49678/*http://smallbusiness.yahoo.com/domain
<http://us.rd.yahoo.com/evt=49678/*http://smallbusiness.yahoo.com/domain
> 
> s/?p=BESTDEAL> 
> Have a HUGE year through Yahoo! Small Business.
>
<http://us.rd.yahoo.com/evt=49678/*http://smallbusiness.yahoo.com/domain
<http://us.rd.yahoo.com/evt=49678/*http://smallbusiness.yahoo.com/domain
> 
> s/?p=BESTDEAL>
>



 

Reply via email to