adding the notion of existence -- postfixing an expression with a question mark will check if to see if it's not null or undefined

This commit is contained in:
Jeremy Ashkenas
2010-01-01 12:31:05 -05:00
parent 3489eec6ee
commit ff80f8d423
2 changed files with 19 additions and 1 deletions

View File

@@ -715,6 +715,19 @@ module CoffeeScript
end
end
# Check an expression for existence (meaning not null or undefined).
class ExistenceNode < Node
attr_reader :expression
def initialize(expression)
@expression = expression
end
def compile(o={})
write("(#{@expression.compile(super(o))} != undefined)")
end
end
# An extra set of parentheses, supplied by the script source.
# You can't wrap parentheses around bits that get compiled into JS statements,
# unfortunately.