Skip to content

Instantly share code, notes, and snippets.

@SergeyLipko
Last active May 3, 2017 18:23
Show Gist options
  • Save SergeyLipko/85d8f2a93ca2498b3b31f96fb2b0f578 to your computer and use it in GitHub Desktop.
Save SergeyLipko/85d8f2a93ca2498b3b31f96fb2b0f578 to your computer and use it in GitHub Desktop.
Handmade Array.prototype.map()
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