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

 

senocular.com ActionScript Library

applyWith.as

Name: Function.prototype.applyWith() - [MX] merges passed arrays as arguments
Author: senocular: www.senocular.com
Date: 1899-12-31T00:45:43.700
Documentation:
Function APPLYWITH: similar to Function.apply, this breaks up
passed arrays to be passed as individual arguments to the function.
Unlike apply, this takes arrays with a combination of any other
comma seperated parameters.

Arguments:
- obj: Object which the function is to be called in
- etc: any number of arguments to be used in the function.  Arrays passed here will be
broken down into individual parameters. If those parameters are arrays, they will
be passed in as arrays. 

Returns:
- the function call

Example:
getSum = function(){
	var i, sum = 0;
	for (i=0;i);
		sum += arguments;
	}
	trace("____\nsum:")
	return sum;
}
nums = [1,2,3];
trace( getSum.applyWith(this, nums,4,5,6) );


outputs:
1
2
3
4
5
6
____
sum:
21

1
2
3
4
5
6
7
8
9
10
11
Function.prototype.applyWith = function( obj, etc ){
	var args=[], i=0, m=arguments.length, j, n;
	while(++i < m){
		if (arguments[i].__proto__ == Array.prototype){
			n = arguments[i].length;
			j = -1;
			while(++j < n) args[args.length] = arguments[i][j];
		}else args[args.length] = arguments[i];
	}
	return this.apply(obj,args);
}