Skip to content

Instantly share code, notes, and snippets.

@itsthatguy
Created September 21, 2017 22:12
Show Gist options
  • Save itsthatguy/8d257ffe7150453cb3b729c37aa9ddd3 to your computer and use it in GitHub Desktop.
Save itsthatguy/8d257ffe7150453cb3b729c37aa9ddd3 to your computer and use it in GitHub Desktop.
var userPreferencesDict = [
'favoriteFood',
'favoriteColor',
'favoriteNumber',
];
var userPreferences = [
'pizza',
'seafoam green',
'42'
];
function Dictionary (dict, array) {
return new Proxy([dict, array], {
get ([keys, values], key) {
const index = keys.indexOf(key);
return values[index];
}
});
}
const preferences = new Dictionary(userPreferencesDict, userPreferences);
preferences.favoriteFood; // => pizza
preferences.favoriteColor; // => seafoam green
preferences.favoriteNumber; // => 42
function foo () {
// 0: Favorite Food
// 1: Favorite Color
// 2: Favorite Number
var userPreferences = [
'pizza',
'seafoam green',
'42'
];
}
var userPreferences = [
'pizza',
'seafoam green',
'42'
];
userPreferences[0] // Favorite Food
userPreferences[1] // Favorite Color
userPreferences[2] // Favorite Number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment