That is, of course, not the same forEach that I created.
I don't like the built in forEach very much. It just doesn't make intuitive sense that the this in the callback function has to refer to the second argument of the method.
It makes it feel like the callback is a method *ON* that second argument. Which I guess it is. But it's just weird.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 01-05-2013 at 04:00 AM..
Yes, but if it is omitted then this in the callback refers to the window! About as useless a choice as possible, methinks.
It was probably considered to provide more versatility, especially as a reference to the array is still provided via one of the parameters passed to the callback.
Isn't this in forEach potentially useful though? For example, to multiply two arrays, perhaps in reverse order:
Code:
function multReverse(element, index, array) {
var theEnd = theEnd || this.length - 1;
this[theEnd - index] *= element;
}
var a = [ 2, 5, 9 ], b = [ 3, 4, 5 ];
a.forEach(multReverse, b);
console.log(a); // [ 2, 5, 9 ]
console.log(b); // [ 27, 20, 10 ]
But this could also be an object. Perhaps even a NodeList . Perhaps we could use it to store a NodeList in an array and do array-like things with it(?).
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
We could use it to create list and dictionary comprehensions . Or our own slice() method:
Code:
function listSlice(element, index, array) {
if (!this[2]) this[2] = array.length - 1;
if (index >= this[1] && index <= this[2]) {
this[0].push(element);
}
}
var a = [ 2, 5, 9, 8, 6 ], b = [];
a.forEach(listSlice, [b, 2]);
console.log(a); // [ 2, 5, 9, 8, 6 ]
console.log(b); // [ 9, 8, 6 ]
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 01-06-2013 at 12:50 AM..
While I agree that the .forEach as defined by 'Old Pedant' has a bunch of potential,
it appears for the simple action I wanted, it is a bit of overkill.
On my machine the following pseudo execution speed test favors the simple function,
just as 'Old Pedant' predicted.
---------------------------------
execution speed test:
During execution, filter won't give you access to the array it generates, but forEach lets you specify the object treated as this:
so does filter and map ...
Quote:
Originally Posted by jmrker;
I was trying to add 1 to each element of the original array
And see that the new array had values all incremented by one.
I thought I could avoid a for...loop with a filter() command.
then simply use .map() instead of .filter(). if your array has a name, you can name it in arguments[1] on the map() call so that it's called "this" within the function, or as arguments[2] inside the function...
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%