Created
October 11, 2018 16:51
-
-
Save StevenLangbroek/c8c0abcacb9bce3b276e84f587d53674 to your computer and use it in GitHub Desktop.
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
/** | |
* It's not hard to imagine JavaScript not having ternary statements, | |
* and having to implement these in userland. What would that look like? | |
* @param leftOrRight a maybe truthy value. | |
* @param left if truthy | |
* @param right otherwise | |
*/ | |
function ternary(leftOrRight, left, right) { | |
if (leftOrRight) { | |
return left; | |
} | |
return right; | |
} | |
/** | |
* So: | |
* `prop.a || prop.b || prop.c ? 1 : 0` | |
* Becomes: | |
* `ternary(prop.a || prop.b || prop.c, 1, 0);` | |
* That whole first statement is a single value: | |
* `true` | |
* Because prop.a is falsy, but prop.b is truthy. | |
* Incidentally: | |
* You'd have to verify, but I think whether or | |
* not prop.c is even evaluated in this case is | |
* specified in the language. | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment