Skip to content

Instantly share code, notes, and snippets.

@MatMercer
Last active December 27, 2019 02:21
Show Gist options
  • Save MatMercer/29a4816321b162e5ef9dc902d3efa6a1 to your computer and use it in GitHub Desktop.
Save MatMercer/29a4816321b162e5ef9dc902d3efa6a1 to your computer and use it in GitHub Desktop.
Simple way of having a clean code in JS using object literals
var capitalByState = {
'MG': 'Belo Horizonte',
'PR': 'Curitiba',
'SP': 'São Paulo'
} // Allocated only one time and the data isn't INSIDE the function
// so 'capitalByState' can come from a external source!
var States = {};
States.getCapitalCity = state => capitalByState[state] || 'Not Found'; // Again, allocated only one time, simulates static
States.getCapitalCity('PR') // Curitiba
States.getCapitalCity('??') // Not Found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment