Last active
May 12, 2018 20:09
-
-
Save ancms2600/2d6d8783793545c09cb670cefbce6f44 to your computer and use it in GitHub Desktop.
Sneaky single-line temporary scoping of variable declarations and cache-reuse (Javascript trivia)
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
// e.g., when you want parameters passed in a pre-sorted order, but you don't want to impose on the developer | |
{ // shadow to avoid mutating input | |
// three examples computing Math.min() just once, though two assignments: | |
const toZero = Math.min(a,c), a = a - toZero, c = c - toZero; // toZero still in scope by EOL (wasteful?) | |
with({toZero:Math.min(a,c)}) const [a,c] = [a-toZero,c-toZero]; // toZero out of scope by EOL (discouraged in "use strict" mode) | |
const [a,c] = [a,c].map((toZero=>v=>v-toZero)(Math.min(a,c))); // toZero out of scope by EOL | |
const [a,c] = (()=>{ const toZero = Math.min(a,c); return [a-toZero,c-toZero]; })(); // toZero out of scope by EOL (long-winded) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a collection of possibly the worst coding practices known to man, all in one place! 😱