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

 

senocular.com ActionScript Library

last.as

Name: last() The last element of the array, get, set or overwrite
Author: senocular: www.senocular.com
Date: 1899-12-31T00:52:28.200
Documentation:
Array LAST:  Simple, but at times useful - last acts as
a type of push alternative as well as a method for 
retrieving the last element of an array.

myArray.last(toAdd, overwrite);

Arguments:
- toAdd: (optional) value to add to as the last element of the array.
- overwrite: (optional, boolean) a true or false value specifying whether or not the
toAdd value should replace the current last element or not. default false (meaning
it will not be replaced and toAdd will be pushed).

Returns:
- the last element of the array.  If a new element is added, the original 'last'
element is returned.

Example:
myArray = [1,2,3,4,5];
trace(myArray.last()); // 5
myArray.last(6);
trace(myArray); // 1,2,3,4,5,6
myArray.last(0, true);
trace(myArray); // 1,2,3,4,5,0

1
2
3
4
5
6
7
8
Array.prototype.last = function(toAdd, overwrite){
	if (toAdd != undefined){
		var old = this[this.length-1];
		this[this.length-overwrite] = toAdd;
		return old;
	}
	return this[this.length-1];
};