Skip to content

Instantly share code, notes, and snippets.

@jed
Forked from 140bytes/LICENSE.txt
Created June 17, 2011 14:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jed/1031568 to your computer and use it in GitHub Desktop.
Save jed/1031568 to your computer and use it in GitHub Desktop.
polyfill an ES5-ish Array.prototype.map
// based loosely on Kris Kowal's es-5shim.js
// https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js#L204
//
// due to space constraints, this version does not check function type or cast length to a number
[].map || ( // if arrays have no map
Array.prototype.map = // set the prototype's map
function( // to a function
a // that takes a mapping function
/*, thisp */ // and an optional scope.
){
for (
var b = this // cache `this` and
, c = b.length // the array's length,
, d = [] // create the return array
, e = 0 // and initialize the cursor,
, f // and cache undefined.
; e < b; // while the cursor is less than the length
) d[e] = // set the result member
e in b // if it originally exists,
? a.call( // to the given function, called with
arguments[1], // the optional scope,
b[e], // existing member,
e++, // member index, and
b ) // current scope,
: f; // or to undefined otherwise.
return d // return the result.
})
[].map||(Array.prototype.map=function(a){for(var b=this,c=b.length,d=[],e=0,f;e<b;)d[e]=e in b?a.call(arguments[1],b[e],e++,b):f;return d})
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Jed Schmidt <http://jed.is>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "map",
"description": "polyfill an ES5-compatibile Array.prototype.map where needed.",
"keywords": [
"array",
"map",
"es5",
"polyfill"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>2,4,6,8</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var map =
[].map||(Array.prototype.map=function(a){for(var b=this,c=b.length,d=[],e=0,f;e<b;)d[e]=e in b?a.call(arguments[1],b[e],e++,b):f;return d})
document.getElementById( "ret" ).innerHTML = map.call([1,2,3,4], function(x){ return x*2 })
</script>
@xavi-
Copy link

xavi- commented Jun 21, 2011

Out of curiosity, why are you referring to the the this-pointer using arguments[1]? Why not add another parameter to the function? The only difference I can see is that it would cause Array.prototype.map.length to return a different value, but is that really a big deal?

@jed
Copy link
Author

jed commented Jun 21, 2011

exactly, that's the point. wherever possible, the shim should have the same properties as the native implementation.

@atk
Copy link

atk commented Jul 23, 2011

@jed : in case you missed it: there's a superfluous comma at the end of the keywords array in the package.json - since fixing all of them will be a constant struggle, how do you like the idea of a simple welformJSON-Function that removes comments and superflous commas?

function(a){return a.replace(/\s*\/\/.*?(?=\n)|\s*\/\*.*?\*\/|,(?=\s*[\]\}])/,'')}

@jed
Copy link
Author

jed commented Jul 24, 2011

you should spin this off into a 140-byte JSONifyer that also quotes object keys and replaces double quotes with single quotes.

@jdalton
Copy link

jdalton commented Aug 21, 2011

This fallback is not ES5 compliant.

@jed
Copy link
Author

jed commented Aug 21, 2011

well aware of that,@jdalton. we just do what we can in the space we're given.

@jdalton
Copy link

jdalton commented Aug 21, 2011

@jed the description polyfill an ES5-compatibile Array.prototype.map is misleading.

@jed
Copy link
Author

jed commented Aug 21, 2011

keep reading: due to space constraints, this version does not check function type or cast length to a number

let's agree to say it's 80% polyful.

@jdalton
Copy link

jdalton commented Aug 21, 2011

There is more wrong with this gist than the disclaimer mentions as the snippet handles spare arrays incorrectly too.
Besides, adding methods to native prototypes that don't comply with spec is irresponsible.

@jed
Copy link
Author

jed commented Aug 21, 2011

wait, you do realize that when someone uses this code it doesn't change the native prototypes in your browser too, right?

@jed
Copy link
Author

jed commented Aug 21, 2011

maybe i give people too much credit, but doubt anyone expects a 140-byte implementation of a native prototype method to be a 100% spec-compliant drop-in replacement.

@jdalton
Copy link

jdalton commented Aug 21, 2011

wait, you do realize that when someone uses this code it doesn't change the native prototypes in your browser too, right?

The snippet suggests [].map||(Array.prototype.map=function(a){..}) which will modify the Array.prototype when the native method doesn't exist. Because the fallback doesn't follow spec it introduces inconsistencies between browsers that do and don't support native Array#map.

maybe i give people too much credit, but doubt anyone expects a 140-byte implementation of a native prototype method to be a 100% spec-compliant drop-in replacement.

I think simply changing the description of the gist to avoid confusion would do.

@atk
Copy link

atk commented Oct 25, 2011

How about we try to get this to 100% compliance? Like this:

[].map||(Array.prototype.map=function(a,t){for(var c=this,b=c.length,d=[],e=0;e<b;)e in c&&(d[e]=a.call(t,c[e],e++,c));d.lengh=b;return d})

@Daniel-Hug
Copy link

@atk Found a typo in your version (you misspelled length as lengh). So just in case anyone wishes to use your version of the snippet, here it is fixed:

[].map||(Array.prototype.map=function(a,t){for(var c=this,b=c.length,d=[],e=0;e<b;)e in c&&(d[e]=a.call(t,c[e],e++,c));d.length=b;return d})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment