Reputation: 1593
I'm a little unsure of how to do this in coffeescript. Right now we have some code that sets a variable conditionally:
myCar.sparkPlug= options.sparkPlug if options.sparkPlug?
However, it's now possible the sparkPlug
property may be initialized with another value:
myCar.sparkPlug = options.glowPlug if options.glowPlug?
What I want to do is set myCar.sparkPlug
to the value of options.SparkPlug
, but if options.glowPlug
exists, use that. (It's possible the options
object will have both a sparkPlug
and glowPlug
property.)
I can do it by listing the two lines of code above in order, but is there a more elegant way to do it?
Upvotes: 1
Views: 38
Reputation: 10680
If I understand, this is what You need:
myCar.sparkPlug = if options.glowPlug? then options.glowPlug else options.SparkPlug
or more complex:
myCar.sparkPlug = if options.glowPlug? then options.glowPlug else (if options.SparkPlug? then options.SparkPlug)
Upvotes: 1