Created
February 6, 2020 03:07
-
-
Save greathmaster/2112128f3ff13629b7e0e0f45a2d8902 to your computer and use it in GitHub Desktop.
Javascript example of closure
This file contains hidden or 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
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