Re:Say What You Mean (in JavaScript)
by TrevorBurnham on Tuesday May 18, 2010 @05:38PM
(#1359201)
But that's not the code I was criticizing. In fact, it's precisely because that's the idiom I associate with boolean operators in JavaScript that the code snippet I posted struck me as unintuitive.
As to "pushing some strange variant": Guilty as charged. Have you tried CoffeeScript? It's fantastic. The code you gave above would be written as
(x) ->
x ||= someDefaultValue> Have you tried CoffeeScript?
Nope. I'm apprehensive about adding another layer to the development process when I know JS very well and don't find it at all limiting. So I'm probably not part of your target audience.
Re:Say What You Mean (in JavaScript)
by TrevorBurnham on Tuesday May 18, 2010 @11:53PM
(#1360050)
I was skeptical at first too, and I wouldn't recommend it to anyone who doesn't already have a pretty good understanding of JavaScript. But I'd urge you to try it for a week or so. It's not a layer of abstraction like GWT; it's just a nice, succinct syntax that compiles statement-by-statement to JavaScript.
Re:Say What You Mean (in JavaScript)
by aaronblohowiak on Tuesday May 18, 2010 @04:54PM
(#1359041)
i much prefer x || (x = someDefaultValue) because it doesn't require backtracking. by that i mean, when i read x = x || somedefaultValue, i am thinking x = "okay, we are in an assignment) and then i see x || and then i have to adjust and think "oh, well, this is a conditional so now i have to think about that situation."
with x || (x = someDefaultValue), what you read is how it is evaluated. if x is true, the rhs is never evaluated. with the above, the assignment happens either way.
If
for setting non-falsy default argument values isn't a JavaScript idiom, I don't know what is.