CoffeeScript doesn't have a C-style ?:
ternary so this:
!@valid() ? 'disabled' : ''
is parsed as:
!@valid() ? ({ 'disabled' : '' })
which is an existential operator:
The Existential Operator
It's a little difficult to check for the existence of a variable in JavaScript. if (variable)
... comes close, but fails for zero, the empty string, and false. CoffeeScript's existential operator ?
returns true unless a variable is null or undefined, which makes it analogous to Ruby's nil?
combined with an object literal. Hence the odd looking JavaScript you're seeing.
CoffeeScript uses if
expressions instead of ?:
:
CoffeeScript can compile if statements into JavaScript expressions, using the ternary operator when possible, and closure wrapping otherwise. There is no explicit ternary statement in CoffeeScript — you simply use a regular if statement on a single line.
If you'd say this in JavaScript:
!this.valid() ? 'disabled' : ''
then you'd say this in CoffeeScript:
if !@valid() then 'disabled' else ''
and since "#{...}"
works with an expression:
className: "btn btn-primary #{if @valid() then 'disabled' else ''}"