Re: [Flashcoders] Delegate and [self] Argument
Thanks for your reply Steve. I modified the original class to do exactly what you suggested with such a third parameter. You also mention, This was added mainly for the purpose of making it easy to remove event listeners without having to keep an external reference to the delegate functions that were created. Do you have an example of how this might be used? How does the reference to the delegate class itself help allow removal of such event listeners? Or wait--- it's a reference to the function passed in as the second parameter... I'll verify that right now. Jeff. On 7/4/06, Steve Webster [EMAIL PROTECTED] wrote: Hi Jeff, In the delegate class from Steve Webster, there is this line: var fullArgs:Array = arguments.concat(self.extraArgs, [self]); I was wondering if anyone knows why there is a reference to self passed to the handler function. Or how might this be useful? This extra argument could cause a problem depending upon what your hander function does with parameters, for example if you have a function with a dynamic number of variables. Yeah, my Delegate class doesn't work very well for event handlers that expect a variable number of arguments. This has caused us problems in the past, and we have an internal version of this class that allows you to pass a third argument to Delegate.create that prevents the reference to self being added to the arguments list. Let me know if this might be useful for you and I'll dig it out. The reference to self is passed so that you have a clean and easy way to reference the delegate function in the event handler method. This was added mainly for the purpose of making it easy to remove event listeners without having to keep an external reference to the delegate functions that were created. Of course, you could just use arguments.caller, but that's less obvious. Cheers, Steve -- Steve Webster http://dynamicflash.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
Re: [Flashcoders] Delegate and [self] Argument
Hi Jeff, In the delegate class from Steve Webster, there is this line: var fullArgs:Array = arguments.concat(self.extraArgs, [self]); I was wondering if anyone knows why there is a reference to self passed to the handler function. Or how might this be useful? This extra argument could cause a problem depending upon what your hander function does with parameters, for example if you have a function with a dynamic number of variables. Yeah, my Delegate class doesn't work very well for event handlers that expect a variable number of arguments. This has caused us problems in the past, and we have an internal version of this class that allows you to pass a third argument to Delegate.create that prevents the reference to self being added to the arguments list. Let me know if this might be useful for you and I'll dig it out. The reference to self is passed so that you have a clean and easy way to reference the delegate function in the event handler method. This was added mainly for the purpose of making it easy to remove event listeners without having to keep an external reference to the delegate functions that were created. Of course, you could just use arguments.caller, but that's less obvious. Cheers, Steve -- Steve Webster http://dynamicflash.com ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
[Flashcoders] Delegate and [self] Argument
Hi All, In the delegate class from Steve Webster, there is this line: var fullArgs:Array = arguments.concat(self.extraArgs, [self]); I was wondering if anyone knows why there is a reference to self passed to the handler function. Or how might this be useful? This extra argument could cause a problem depending upon what your hander function does with parameters, for example if you have a function with a dynamic number of variables. I'm debating whether I should keep this [self] reference in a helper function I wrote (see below), that is similar to delegate. JJ Here's the short helper function I wrote: it executes a function after a specified delay and repeats the call if the optional 'repeat' variable is passed in // // Two ways to use this function //delay( timeDelay, target, handler ) //delay( timeDelay, repeat, target, handler ) public static function delay( timeDelay:Number ):Void { // Create the timeDelay function var timerDelay:Function = function() { // Get reference to self var self:Function = arguments.callee; // Augment arguments passed from broadcaster with additional args // Note: The [self] array addition passes a reference to the callee itself and passes // this as a parameter to the handler which could cause unforseen problems. It is not // clear to me why delegate passes this reference. var fullArgs:Array = arguments.concat(self.extraArgs, [self]); self.repeat--; if( self.repeat == 0 ) { clearInterval( self.intervalId ); } // Call handler with arguments return self.handler.apply(self.target, fullArgs); }; // Pass in local references // The arguments passed in have different meaning whether we have // repeat variable or not if( typeof( arguments[1] ) == number ) { timerDelay.repeat = arguments[1]; timerDelay.target = arguments[2]; timerDelay.handler = arguments[3]; timerDelay.extraArgs = arguments.slice(4); } else { timerDelay.repeat = 1; timerDelay.target = arguments[1]; timerDelay.handler = arguments[2]; timerDelay.extraArgs = arguments.slice(3); } // start the timer interval timerDelay.intervalId = setInterval( timerDelay, timeDelay ); } ___ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com