Created
July 20, 2010 10:51
-
-
Save edulan/482809 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Extensions to the AS2 Array class | |
*/ | |
class ArrayExtensions | |
{ | |
public function ArrayExtensions() { | |
} | |
public static function initialize() { | |
Array.prototype.map = function(mapFunction:Function):Array { | |
var result:Array = new Array(); | |
var len:Number = this.length; | |
for (var i:Number = 0; i < len; i++) { | |
result.push(mapFunction(this[i])); | |
} | |
return result; | |
} | |
Array.prototype.reduce = function(initValue:Object, reduceFunction:Function):Object { | |
var result:Object = initValue; | |
var len:Number = this.length; | |
for (var i:Number = 0; i < len; i++) { | |
result = reduceFunction(result, this[i]); | |
} | |
return result; | |
} | |
} | |
} | |
Usage: | |
ArrayExtensions.initialize(); | |
var sum = [1,2,3,4].map(function(n) { return n*2 } ).reduce(0, function(a,n) { return a+=n } ); | |
trace(sum) // Output 20; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice code dude I will use it for sure...