Forum Discussion
nicktulett
12 years agoContributor
Even if you do extend the array prototype it will only apply in that single unit.
What will work is defining iteration functions (that can be global) that take an array argument.
For instance I have a MAP function that I use as a general iterator, stolen from
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
function MAP(arr, callback, thisArg) {
var T, A, k;
if (arr == null) {
return false;
}
var O = Object(arr);
var len = O.length >>> 0;
if (typeof callback != "function") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
A = new Array(len);
k = 0;
while(k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[ k ];
mappedValue = callback.call(T, kValue, k, O);
A[ k ] = mappedValue;
}
k++;
}
return A;
}
What will work is defining iteration functions (that can be global) that take an array argument.
For instance I have a MAP function that I use as a general iterator, stolen from
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
function MAP(arr, callback, thisArg) {
var T, A, k;
if (arr == null) {
return false;
}
var O = Object(arr);
var len = O.length >>> 0;
if (typeof callback != "function") {
throw new TypeError(callback + " is not a function");
}
if (thisArg) {
T = thisArg;
}
A = new Array(len);
k = 0;
while(k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[ k ];
mappedValue = callback.call(T, kValue, k, O);
A[ k ] = mappedValue;
}
k++;
}
return A;
}