Created
October 9, 2014 10:42
-
-
Save abozhilov/cb4710ddc1c1f4d1a28d to your computer and use it in GitHub Desktop.
Array.from simple polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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