Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

addIn.as

Name: Function.prototype.addIn() - (MX) adds more function calls to one function
Author: senocular: www.senocular.com
Date: 1899-12-31T00:15:49.200
Documentation:
Function ADDIN (Flash MX): takes one function and adds into
it other functions. When that function is called again, its
original actions are called as well as the actions of the added
function(s).  The arguments passed to the original function are also
passed to the new added function(s) along with (appended at the end)
any return value the original function gave back.  The original
function call is kept in a property of that function called
_defaultDef
 
Arguments:
- funcs: any number of comma seperated functions to be added into the call
of the original function.
 
Returns:
- returns the new compound function which itself returns the return value of the last
function added in this manner

Example:
function Primary(args){
	for (all in arguments) trace("primary: arg"+all +" = \""+ arguments[all]+"\"");
	return "throw this back";
}
function Secondary(args){
	for (all in arguments) trace("secondary: arg"+ all +" = \""+ arguments[all]+"\"");
	return arguments[arguments.length-1] + " again";
}
 
myPrim = Primary("a","b");
// primary: arg1 = "b"
// primary: arg0 = "a"
 
trace("returned "+myPrim);
// returned throw this back
 
Primary = Primary.addIn(Secondary);
myNewPrim = Primary("a","b");
// primary: arg1 = "b"
// primary: arg0 = "a"
// secondary: arg2 = "throw this back"
// secondary: arg1 = "b"
// secondary: arg0 = "a"
 
trace("returned "+myNewPrim);
// returned throw this back again
 
myOrigPrim = Primary._defaultDef("a","b");
// primary: arg1 = "b"
// primary: arg0 = "a"
 
trace("returned "+myOrigPrim);
// returned throw this back
 
Primary = Primary._defaultDef
Primary("no more");
// primary: arg0 = "no more"

clip.incX = function(){
	this._x++;
}
clip.incY = function(){
	this._y++;
}
clip.incScale = function(){
	this._xscale = this._yscale++;
}
funcs = [clip.incX, clip.incY, clip.incScale]
clip.onEnterFrame = function(){}
clip.onMouseDown = function(){ // add functions to enterFrame 1 by 1
	this.onEnterFrame = this.onEnterFrame.addIn(funcs.shift());
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// version 1 - all functions get all arguments with, added on, the previously returned value
Function.prototype.addIn = function(funcs){
	var orig = this, def = this._defaultDef, args = arguments, i, prop, f = arguments.length;
	var fun = function(){
		var l = arguments.length;
		var r = orig.apply(this, arguments);
		for (i=0;i<f;i++){
			if (r != undefined) arguments[l] = r;
			r = args[i].apply(this, arguments);
		}
		return r;
	}
	for (prop in this) if (this[prop] != this.__proto__[prop]) fun[prop] = this[prop];
	fun._defaultDef = (def) ? def : orig;
	return fun;
}

//version 2 - all added functions get only the previously returned values as arguments
Function.prototype.addIn = function(funcs){
	var orig = this, def = this._defaultDef, args = arguments, i, prop, f = arguments.length;
	var fun = function(){
		var l = arguments.length;
		var r = orig.apply(this, arguments);
		for (i=0;i<f;i++) r = args[i].call(this, r);
		return r;
	}
	for (prop in this) if (this[prop] != this.__proto__[prop]) fun[prop] = this[prop];
	fun._defaultDef = (def) ? def : orig;
	return fun;
}