Tweet For Code #4
You don't need a thousand lines of code to make a big difference in any coding language. Oftentimes it's quite the opposite: a few tiny code snippets can do a world of good and accomplish big things. I asked my Twitter followers to tweet to me their favorite tiny snippets of code -- that's a bit difference to try to pack into 140 characters! Here are my favorites from this round!
ES6 Array Initialization
Here are a few clever ways to initialize an array with ES6:
@davidwalshblog Initialize an array with a default value (#ES6):
> Array.from({ length: 7 }, _ => 42);
[42, 42, 42, 42, 42, 42, 42]
— Jeremy Martin (@jmar777) November 12, 2014
@davidwalshblog Similarly, initialize w/ 1-N:
> var oneToN = n => Array.from({ length: n }, (_, i) => i + 1);
> oneToN(5);
[1, 2, 3, 4, 5]
— Jeremy Martin (@jmar777) November 12, 2014
Short syntax for dynamic result!
Remove Wrapping HTML with inner/outerHTML
This neat little trick allows you to remove a wrapping element and re-parent the child in one operation.
@davidwalshblog `elem.outerHTML = elem.innerHTML;` removes wrapping `elem`.
— Otto Rask (@ojrask) November 12, 2014
Of course you would brick your event connections but this is a neat trick.
3D Body Elements
Want a 3D look at a page's elements? Check out this sweet JavaScript snippet:
@davidwalshblog t='transform';$('body').css({perspective:999}).css(t,'preserve-3d').children().css(t,'translateZ(-500px) rotateY(30deg)');
— David Khourshid (@DavidKPiano) November 12, 2014
This is a really neat optical effect, though not the most practical of snippets.
YOLO!
Since `sudo` gives you the ultimate of powers, why not give it a YOLO alias?
@davidwalshblog `alias yolo= 'sudo'` is always fun
— Ian Fleming (@iangfleming) November 12, 2014
Hilarious!
Thank you to everyone who participated, and keep your tiny snippets for the next Tweet for Code!