3 ms·
Totally -- I think that 0 (which is a perfectly valid value for a number to be) bites people doing lazy initialization all the time. For this reason, CoffeeScr
by jashkenas 15y ago
Totally -- I think that 0 (which is a perfectly valid value for a number to be) bites people doing lazy initialization all the time.
For this reason, CoffeeScript provides the existential operator, which asks: does this variable exist? (Is it not null or undefined?) You can use it in the same places you would use "||", as well as some others:
# In place of `||`
result = answer ? default
# Does `object.value` exist?
if object.value?
# Lazy initialize the speed of the car:
car.speed ?= 60
# Give me the value of `length`, only if `object` exists.
length = jsonResponse.list.object?.length
- city41 15y agoI still haven't gone over to CoffeeScript. But I must admit Those are some compelling examples. ?= is especially nice (I miss ||= from Ruby)