Skip to content

Instantly share code, notes, and snippets.

@greathmaster
Created February 6, 2020 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greathmaster/2112128f3ff13629b7e0e0f45a2d8902 to your computer and use it in GitHub Desktop.
Save greathmaster/2112128f3ff13629b7e0e0f45a2d8902 to your computer and use it in GitHub Desktop.
Javascript example of closure
Array.prototype.myEach = function(callback) {
for (let i = 0; i < this.length; i++) {
callback(this[i]);
}
}
Array.prototype.myMap = function(callback) {
let newArray = []
const collect = function(el) {
newArray.push(callback(el))
}
this.myEach(collect)
return newArray;
}
const cb = function(e) {
return 2*e
}
console.log([1,2,3,4].myMap(cb))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment