|Problems? Is your data what you think it is?|
Re^2: Rosetta PGA-TRAMby afoken (Parson)
|on Jun 23, 2009 at 21:02 UTC||Need Help??|
foreach was camelCased to forEach, Perl's grep is named filter. There is also a reduceRight method that works from right to left.
The calling conventions pass the current element, the index into the array, and the array object itself to the callback. While one could construct examples where the extra parameters could be useful, I think this "tastes" bad: No callback should need to access (and modify) the original array while iterating over the array. No callback should need to know the current index into the array. If it does, you are probably reinventing push, pop, shift, unshift, or splice.
The new methods allow specifying a different object for this inside the callback, which can be useful sometimes. If you don't specify an object, the array object is used.
Someone also recognised that map could be useful outside array contexts and constructed this piece of code. It just hurts my eyes.
This code works "by accident", because Array.prototype.map can iterate over everything that behaves like an array, not only over real Arrays. The call method available on all Function objects allows to call a method of one object with this set to another object (the first argument passed to call). In this case, this is set to an instance of a String object, over which the Array.prototype.map method iterates.
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)