Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created October 9, 2014 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abozhilov/cb4710ddc1c1f4d1a28d to your computer and use it in GitHub Desktop.
Save abozhilov/cb4710ddc1c1f4d1a28d to your computer and use it in GitHub Desktop.
Array.from simple polyfill
function arrayFrom(arrayLike, mapFn, thisArg) {
var arr = [];
if (typeof mapFn != 'function') {
arr.push.apply(arr, arrayLike);
return arr;
}
for (var i = 0, len = arrayLike.length; i < len; i++) {
arr[i] = mapFn.call(thisArg, arrayLike[i], i);
}
return arr;
}
console.log(arrayFrom([1,,2])); //[ 1, undefined, 2 ]
console.log(arrayFrom([1,,2], function(val) {return val * val;})); //[ 1, NaN, 4 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment