Why doesn't Array.from() pass the array to the mapFn?
T.J. Crowder
tj.crowder at farsightsoftware.com
Wed May 3 11:06:31 UTC 2017
`Array.prototype.map` doesn't pass a reference to the *newly-created* array
either. The third argument `Array.prototype.map` passes the mapping
function is a reference to the *existing* array, not the new one. With
`Array.from`, there is no existing array to pass.
Not having the mapping functions (either of them) get a reference to the
array being constructed avoids an entire class of errors caused by the
mapping function modifying the array as it's being constructed.
-- T.J. Crowder
On Wed, May 3, 2017 at 11:50 AM, the kojoman <kojoman at gmail.com> wrote:
> Array.map(mapFn) calls the mapFn(v,i,a) passing the currentValue,
> currentIndex and the array it is iterating over as arguments.
> I was happy to find out about Array.from(itterable, mapFn, thisArg), but
> unfortunately, the mapping didn't work as expected.
> It seems, Array.from doesn't pass in the newly created array to the mapFn.
> Why is that?
>
> For instance I was expecting Array.from("aabc", (v,i,a) => { if(a[i] ===
> a[i-1]) a.splice(i,1) }) to evaluate to ["a","b","c"], but a is undefined.
> Is there a reason why the mapFn isn't called the same way as an Array.map
> function is?
> -John
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170503/a0cb0fe3/attachment.html>
More information about the es-discuss
mailing list