Created
October 24, 2014 21:33
-
-
Save KamilLelonek/a4b8a27cbfb766b0b5ac to your computer and use it in GitHub Desktop.
Hipster JavaScript usages
This file contains hidden or 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
// Boring | |
if (success) { | |
obj.start(); | |
} else { | |
obj.stop(); | |
} | |
// Hipster-fun | |
var method = (success ? 'start' : 'stop'); | |
obj[method](); | |
// Or even saver with two conditions | |
success ? obj.start() : obj.stop(); | |
/////////////////////////////////////////////////////// | |
// default to 'No name' when myName is empty (or null, or undefined) | |
var name = myName || 'No name'; | |
// make sure we have an options object | |
var doStuff = function(options) { | |
options = options || {}; | |
// ... | |
}; | |
/////////////////////////////////////////////////////// | |
// Boring | |
if (isThisAwesome) { | |
alert('yes'); // it's not | |
} | |
// Awesome | |
isThisAwesome && alert('yes'); | |
// Also cool for guarding your code | |
var aCoolFunction = undefined; | |
aCoolFunction && aCoolFunction(); // won't run nor crash | |
/////////////////////////////////////////////////////// | |
console.time('test'); | |
console.timeEnd('test'); | |
/////////////////////////////////////////////////////// | |
function checkIfNumberIsOdd(number) { | |
return !!(number & 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment