Skip to content

Instantly share code, notes, and snippets.

@oliverwebr
Created June 12, 2020 12:34
Show Gist options
  • Save oliverwebr/cd477910e14ac47032030ed203571b0b to your computer and use it in GitHub Desktop.
Save oliverwebr/cd477910e14ac47032030ed203571b0b to your computer and use it in GitHub Desktop.
JS-Closures Example-1: Simpler APIs
function createTranslation(lang) {
const dictionary = {
food: {
en: "food",
de: "essen",
},
apple: {
en: "apple",
de: "apfel",
},
};
return function t(word) {
return dictionary[word][lang];
};
}
const en = createTranslation("en");
const de = createTranslation("de");
console.log(en("apple"), de("apple"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment