Created
December 8, 2015 11:21
-
-
Save marcohamersma/dc9590f3a8d884f8e921 to your computer and use it in GitHub Desktop.
Thinking about alternate ways of defining a variable based on the value of another
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
// You need to get a new value based on the value of another variable, | |
// an if statement could get a bit messy so you might use a switch: | |
let someVariable; | |
switch (type) { | |
case 'bla': | |
someVariable 'someValue'; | |
break; | |
… | |
} | |
// This syntax is a bit of a bother, first having to define the variable and | |
// adding "break" after evert case. | |
// Actually “returning” a value makes more sense here, but in order to do that | |
// we would need to extract it in a function somewhere, and being indirect: | |
const bla = blaFromType(type); | |
// Ideally, this would work: | |
const someVariable = switch (type) { | |
case 'bla': | |
return 'someValue'; | |
… | |
} | |
// … but it doesn't | |
// Although… with ES6, you could do a self-executing | |
// arrow function and get close to what we would want. | |
const someVariable = (() => { switch(type) { | |
case 'bla': | |
return 'someValue' | |
… | |
}})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment