The expense is in making an "activation frame" (or something like that)
for anonymous functions. I don't have numbers on how much worse the
performance is. It probably doesn't make a significant difference for
occasional events.
 
Nevertheless, when we write framework classes, we almost always use
methods for event handlers. Is there a reason that you prefer to use an
anonymous function?
 
- Gordon

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Troy Gilbert
Sent: Thursday, February 15, 2007 11:59 AM
To: [email protected]
Subject: Re: [flexcoders] Re: addEventListener and additional arguments?



How much worse is the performance? Are we talking on the order of a
dozen or so extra instructions or orders of magnitude more?

And I guess it would depend on the event being handled, right? Sure, it
may be cost-prohibitive for onEnterFrame, but what something like
onMouseDown or even onCreationComplete? 

[Note: I'm using the old style event names as I find it more readable
when discussing them.]

And what's the expensive part? The binding of the anonymous function
object rather than a method, or executing the function object rather
than a method? I would assume the latter, since the former should be
identical (they're both objects, right?). 

And doesn't an event listener just store a collection of Function
objects? So it's already dispatching through a function object, which
makes me think that the overhead is in managing closure, etc?

Troy.



On 2/15/07, Gordon Smith <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> >
wrote: 

        

        Using anonymous functions as event handlers isn't recommended,
as they have worse performance than using methods.
         
        - Gordon

________________________________

        From: [email protected]
[mailto:[email protected] <http://yahoogroups.com> ] On Behalf
Of Troy Gilbert
        Sent: Wednesday, February 14, 2007 9:44 PM
        To: [email protected]
        Subject: Re: [flexcoders] Re: addEventListener and additional
arguments?
        
        
        

        Remember, functions are objects to (so you can treat them like
fancy functors in C++):
        
        

        var functor:Function = function(arg:String):String { return arg
+ functor["param"]; } 
        functor["param"] = "dolly";
        trace(functor("hello"); // outputs "hellodolly"
        

        or, for events:
        
        
        var myEventHandler:Function = function(event:MouseEvent):void {
trace(myEventHandler["extraData"]); }
        myEventHandler["extraData"] = "hellodolly";
        this.stage.addEventListener(MouseEvent.CLICK , myEventHandler);
        // when you click on the stage, you'll get the debug message
"hellodolly"
        


        This'll give you what you're looking for. Not sure what the best
way to copy a "functor" object would be... but you could definitely just
create a new, separately named Function object for each form item that
wraps your function, passing in the the "extraData". 
        
        Troy.
        
        
        
        On 2/14/07, Gordon Smith < [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]> > wrote: 

                

                Here's an example of what I meant:
                 
                <?xml version="1.0" encoding="utf-8"?>
                <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml
<http://www.adobe.com/2006/mxml> "> 
                 
                    <mx:Script>
                    <![CDATA[
                   
                        private var foo:int = 7;
                  
                        private function
clickHandler(event:MouseEvent):void
                        {
                            trace(foo);
                        }
                 
                     ]]>
                     </mx:Script>
                 
                    <mx:Button click="clickHandler(event)"/>
                 
                </mx:Application>
                
                Note that the Button's clickHandler() can access the
instance variable 'foo', despite the fact that 'foo' isn't passed to
clickHandler() and 'foo' isn't a property of event.target, which is the
Button.
                 
                The reason that clickHandler() can access 'foo' is that
when clickHandler() executes, 'this' is the Application (or whatever
component you're writing).
                 
                - Gordon

________________________________

                From: [email protected] [mailto: flexcoders@
<mailto:flexcoders@>  yahoogroups.com <http://yahoogroups.com> ] On
Behalf Of darylgmyers
                Sent: Wednesday, February 14, 2007 7:42 AM
                To: [email protected]
                Subject: [flexcoders] Re: addEventListener and
additional arguments?
                
                
                

                Gordon,
                I'm pretty new to Flex so I appologize for the
additional question.
                
                Are you saying that I can add an instance property to
the target so 
                that it will be available in the event.target? What I'm
actually 
                doing is creating a set of form fields on the fly using
action 
                script. Some of these fields may be number types with a
precision 
                value. My formatting is done with an event listener
which I need to 
                be able to pass the precision value. 
                
                --- In [email protected]
<mailto:flexcoders%40yahoogroups.com> , "Gordon Smith" <[EMAIL PROTECTED]>
wrote:
                >
                > There should be no need to pass additional arguments
to the event
                > handler method, because -- assuming it is indeed a
method of a 
                class --
                > it can access all the instance properties of that
class. So just 
                set an
                > instance property.
                > 
                > - Gordon
                > 
                > ________________________________
                > 
                > From: [email protected]
<mailto:flexcoders%40yahoogroups.com>  
                [mailto: [email protected]
<mailto:flexcoders%40yahoogroups.com> ] On
                > Behalf Of darylgmyers
                > Sent: Tuesday, February 13, 2007 1:42 PM
                > To: [email protected]
<mailto:flexcoders%40yahoogroups.com>  
                > Subject: [flexcoders] addEventListener and additional
arguments?
                > 
                > 
                > 
                > Is there a way to use the addEventListener method and
pass 
                additional 
                > arguments along with the event? I have a form where I
must create 
                the 
                > form objects dynamically so I need to use
addEventListener for 
                > formatting, etc. I can not use inline mxml to add the
listeners.
                >
                
                

                

                


        

        

        

        


 

Reply via email to