Last active
May 5, 2016 18:49
-
-
Save zwhitchcox/6cb0d05a78a7bd19458b6bec797fa671 to your computer and use it in GitHub Desktop.
implementation of map
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 map(arr, op, thisArg) { | |
if (typeof op !== 'function') throw new Error('operation must be a function') | |
if (!Array.isArray(arr)) throw new Error('first argument must be an array') | |
if (typeof thisArg !== 'object' && thisArg !== undefined) throw new Error('`this` argument must be an object') | |
var newArr = arr.slice() | |
op = op.bind((thisArg || null)) | |
for (var i = 0, l = newArr.length; i < l; i++) { | |
newArr[i] = op(newArr) | |
} | |
return newArr | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment