Last active
June 23, 2017 05:07
-
-
Save chriswrightdesign/a1bff07e438786bc196d6908945ec8f7 to your computer and use it in GitHub Desktop.
Map closure
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
// Make our function useful within a mapping function: | |
const prefix = prefix => value => `${prefix}${value}`; | |
// our function takes a prefix string, then returns a function which takes value, | |
// which then returns a string with the prefix and value together. | |
const animals = ['dog', 'cat', 'bird', 'giraffe']; | |
animals.map(prefix('super '); // result: ['super dog', 'super cat', 'super bird']; | |
// Alternatively, we could also name our outer function for clarity: | |
const prefixSuper = prefix('super '); | |
animals.map(prefixSuper); // result: ['super dog', 'super cat', 'super bird']; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment