Reputation: 622
Is there a way to make Array.prototype.map a function and call it as customMap(passitanyarray,function)
In general,
is there a way to get Array.prototype.(whatever)
and take whatever and make it your own function.
for example like
Array.prototype.map(function())
and extract map and use like this map([],function())
instead of Array.prototype.map(function())
I would explain more if that is not clear.
Thanks, In advance
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Polyfill
Upvotes: 0
Views: 36
Reputation: 92440
If you're willing to manage the correct context of this
with the function you could extract the call()
function and provide it with the correct context. It's a little ugly (and I'm not sure it's wise), but it works and should work even with the polyfill:
let arr = [1, 2, 3]
/* get reference to properly bound call() */
let map = Function.call.bind(Array.prototype.map)
/* now you can use it like a regular function */
let double = map(arr, item => item * 2)
console.log(double)
Upvotes: 2
Reputation: 13963
You could wrap the arr.map()
invocation like this:
function map(arr, fn) {
return arr.map(fn);
}
console.log(map([1,2,3], x => x + 1));
Upvotes: 2