Created
November 23, 2011 21:23
-
-
Save jashkenas/1389969 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
// How do you tend to declare variables in your code, when nested in an if / else? | |
// All three are functionally equivalent. | |
// (Ternary is cheating for these purposes.) | |
// Option 1: | |
function() { | |
if (condition) { | |
var attrs = a; | |
} else { | |
var attrs = b; | |
} | |
} | |
// Option 2: | |
function() { | |
var attrs; | |
if (condition) { | |
attrs = a; | |
} else { | |
attrs = b; | |
} | |
} | |
// Option 3: | |
function() { | |
if (condition) { | |
var attrs = a; | |
} else { | |
attrs = b; | |
} | |
} |
@fat that's so pretty
var a = (function(){ return 'value'; }).call(this);
@fat I never, ever, leave braces off when optional in JS. I'll take the line-noise for security. if w/o braces only works when it's at the end of a line a la Ruby.
#2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#2