Last active
May 3, 2017 18:23
-
-
Save SergeyLipko/85d8f2a93ca2498b3b31f96fb2b0f578 to your computer and use it in GitHub Desktop.
Handmade Array.prototype.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 customMap(arr, callback, thisArgs) { | |
let newArr = []; | |
for (let i = 0; i < arr.length; i++) { | |
newArr.push(callback.call(thisArgs, arr[i])); | |
} | |
return newArr; | |
} | |
// Или так | |
Array.prototype._map = function(fn) { | |
let res = []; | |
for (let i = 0; i < this.length; i++) { | |
res.push(fn(this[i])); | |
} | |
return res; | |
} | |
// Filter | |
Array.prototype._filter = function(fn, args) { | |
let res = []; | |
for (let i = 0; i < this.length; i++) { | |
if (fn.call(null, this[i])) { | |
res.push(this[i]); | |
} | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment