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 );
}
_______________________________________________
[email protected]
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