All pastes #336939 Raw Edit

Delegate

public text v1 · immutable
·published 2007-02-02 11:49 UTC
rendered paste body
/* INFO
* @AUTHOR Ramon Tayag
* @DATE Dec. 10, 2005
* @DESCRIPTION This is a modified delegate class that handles more than 2 args.
* @THANKS
* - Lexicon who clarified the create function
*/

class net.quirkworks.utils.Delegate extends Object
{
	private var func:Function;
	
	function Delegate(f:Function)
	{
		func = f;
	}
	
	static function create(o:Object, func:Function):Function
	{
		var f = function()
		{
			var target = arguments.callee.target;
			var myFunc = arguments.callee.func;
			var args = arguments.callee.args;
			
			return myFunc.apply(target, arguments.concat(args));
		};

		f.target = o;
		f.func = func;
		
		if (arguments.length > 2)
		{
			f.args = arguments.slice(2);
		} else
		{
			f.args = [];
		}

		return f;
	}
	
	function createDelegate(o:Object):Function
	{
		return create(o, func);
	}
}