Created
January 16, 2018 00:00
-
-
Save SakoMe/de319a74c7cbf57323e6481311f7b31e to your computer and use it in GitHub Desktop.
Closures...
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
// Global | |
const numbers = [1, 2, 3, 4, 5, 6] | |
const number = (n) => { | |
return numbers[n] | |
} | |
console.log(number(4)) | |
// Slow (must generate an array at every call only to choose one number) | |
const number = (n) => { | |
const numbers = [1, 2, 3, 4, 5, 6] | |
return numbers[n] | |
} | |
console.log(number(1)) | |
// Closure | |
const number = (() => { | |
const numbers = [1, 2, 3, 4, 5, 6] | |
return (n) => { | |
return numbers[n] | |
} | |
})() | |
console.log(number(2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment