var reduceRight = function(array, callbackfn, initialValue){ if(array.reduceRight){ return array.reduceRight(callbackfn, current) } if (!Base.isFunction(callbackfn)) Base.error(callbackfn + " is not a function"); // Pull out the length so that modifications to the length in the // loop will not affect the looping. var i = array.length - 1, current; find_initial: if (arguments.length < 2){ for (; i >= 0; i--){ current = array[i]; if (current !== undefined || i in array){ i--; break find_initial; } } Base.error("reduce of empty array with no initial value"); } for (; i >= 0; i--){ var element = array[i]; if (element !== undefined || i in array){ current = callbackfn.call(null, current, element, i, array); } } return current; };
var forEach = function(array, callbackfn, thisArg){ if(array.forEach){ return array.forEach(callbackfn, thisArg); } if (!Base.isFunction(callbackfn)) Base.error(callbackfn + " is not a function"); // Pull out the length so that modifications to the length in the // loop will not affect the looping. var len = array.length; for (var i = 0; i < len; ++i){ var current = array[i]; if (current !== undefined || i in array){ callbackfn.call(thisArg, current, i, array); } } };
var map = function(array, callbackfn, thisArg){ if(array.map){ return array.map(callbackfn, thisArg); } if (!Base.isFunction(callbackfn)) Base.error(callbackfn + " is not a function"); // Pull out the length so that modifications to the length in the // loop will not affect the looping. var len = array.length, result = new Array(len); for (var i = 0; i < len; ++i){ var current = array[i]; if (current !== undefined || i in array){ result[i] = callbackfn.call(thisArg, current, i, array); } } return result; };