Reducing Map: jQuery vs jQuery vs JavaScript
mrspeaker.net
mrspeaker.net
One thing that sucks about JS currently is the fact that you have to do Array.prototype.map.call(a, ...) instead of a.map, and even then you don't have a guarantee that the correct function will execute. I know in ECMAScript 5 you can freeze properties of an object, but unless prototypes of basic methods come frozen, it's not ideal.
If that's the case, none of the basic objects are safe, not just Array..
But if you're unfortunate enough to have to care about the ol' stuff, there are some nice polyfills on each of the MDC map/reduce/filter/etc pages: https://developer.mozilla.org/en/JavaScript/Reference/Global...
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
res[i] = fun.call(thisp, t[i], i, t);
}
return res;
};
}Are you going to compare performance?
It's got the nice feature of providing synchronous and asynchronous maps, reduces, filters and everything else you'd expect.