Skip to content

Instantly share code, notes, and snippets.

@zwhitchcox
Last active May 5, 2016 18:49
Show Gist options
  • Save zwhitchcox/6cb0d05a78a7bd19458b6bec797fa671 to your computer and use it in GitHub Desktop.
Save zwhitchcox/6cb0d05a78a7bd19458b6bec797fa671 to your computer and use it in GitHub Desktop.
implementation of map
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