1 /** 2 * @class The built-in Array class. 3 * @name Array 4 */ 5 6 if (!Array.prototype.map) { 7 /** 8 * Creates a new array with the results of calling a provided function on 9 * every element in this array. Implemented in Javascript 1.6. 10 * 11 * @see <a 12 * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/Map">map</a> 13 * documentation. 14 * @param {function} f function that produces an element of the new Array from 15 * an element of the current one. 16 * @param [o] object to use as <tt>this</tt> when executing <tt>f</tt>. 17 */ 18 Array.prototype.map = function(f, o) { 19 var n = this.length; 20 var result = new Array(n); 21 for (var i = 0; i < n; i++) { 22 if (i in this) { 23 result[i] = f.call(o, this[i], i, this); 24 } 25 } 26 return result; 27 }; 28 } 29 30 if (!Array.prototype.filter) { 31 /** 32 * Creates a new array with all elements that pass the test implemented by the 33 * provided function. Implemented in Javascript 1.6. 34 * 35 * @see <a 36 * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/filter">filter</a> 37 * documentation. 38 * @param {function} f function to test each element of the array. 39 * @param [o] object to use as <tt>this</tt> when executing <tt>f</tt>. 40 */ 41 Array.prototype.filter = function(f, o) { 42 var n = this.length; 43 var result = new Array(); 44 for (var i = 0; i < n; i++) { 45 if (i in this) { 46 var v = this[i]; 47 if (f.call(o, v, i, this)) result.push(v); 48 } 49 } 50 return result; 51 }; 52 } 53 54 if (!Array.prototype.forEach) { 55 /** 56 * Executes a provided function once per array element. Implemented in 57 * Javascript 1.6. 58 * 59 * @see <a 60 * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/ForEach">forEach</a> 61 * documentation. 62 * @param {function} f function to execute for each element. 63 * @param [o] object to use as <tt>this</tt> when executing <tt>f</tt>. 64 */ 65 Array.prototype.forEach = function(f, o) { 66 var n = this.length >>> 0; 67 for (var i = 0; i < n; i++) { 68 if (i in this) f.call(o, this[i], i, this); 69 } 70 }; 71 } 72 73 if (!Array.prototype.reduce) { 74 /** 75 * Apply a function against an accumulator and each value of the array (from 76 * left-to-right) as to reduce it to a single value. Implemented in Javascript 77 * 1.8. 78 * 79 * @see <a 80 * href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/Reduce">reduce</a> 81 * documentation. 82 * @param {function} f function to execute on each value in the array. 83 * @param [v] object to use as the first argument to the first call of 84 * <tt>t</tt>. 85 */ 86 Array.prototype.reduce = function(f, v) { 87 var len = this.length; 88 if (!len && (arguments.length == 2)) { 89 throw new Error(); 90 } 91 92 var i = 0; 93 if (arguments.length < 3) { 94 while (true) { 95 if (i in this) { 96 v = this[i++]; 97 break; 98 } 99 if (++i >= len) { 100 throw new Error(); 101 } 102 } 103 } 104 105 for (; i < len; i++) { 106 if (i in this) { 107 v = f(v, this[i], i, this); 108 } 109 } 110 return v; 111 }; 112 } 113