Skip to content

Instantly share code, notes, and snippets.

@MasterXen
Forked from twolfson/LICENSE.txt
Created March 18, 2013 20:45
Show Gist options
  • Save MasterXen/5190650 to your computer and use it in GitHub Desktop.
Save MasterXen/5190650 to your computer and use it in GitHub Desktop.

Ugly forEach polyfill for primitives

  • no index argument
  • no objects in array
  • no strings in array with ","
function (array, iterator) {
// array = [1,2,3,4,,"pewpew"];
// array.toString() -> "1,2,3,4,,pewpew"
// string.replace(/[^,]+/g, iterator);
// iterator(item);
(array+'').replace(/[^,]+/g, iterator);
}
function(a,i){(a+'').replace(/[^,]+/g,i)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
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": "Ugly forEach",
"description": "Ugly forEach polyfill for primitives",
"keywords": [
"polyfill"
]
}
<!DOCTYPE html>
<title>Ugly forEach</title>
<div>Expected value: <b>246NaN</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var myFunction = function(){
var result = "";
return (function(a,i){(a+'').replace(/[^,]+/g,i)})
([1,2,3,,,"pewpew"], function (item) {
result += item * 2;
}), result; // "246NaN"
}
document.getElementById( "ret" ).innerHTML = myFunction();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment