> Right. Maybe the operator should have a different name. "isDefined"? "has a
> value" expressed as an operator name?
>
> isDefined x
>
> would be syntactic sugar for
>
> typeof x !== "undefined" && x !== null
>
> The expression would not throw an exception if x hasn’t been declared.
>
>
> What about adding CoffeeScript's existential operator? It behaves in the
> exact same way - `x?` desugars into `typeof x !== "undefined" && x !== null`.
> Could be nice to have that as part of ES.next.
>
> One more important reason to add this (whether as `x?` or as a regular
> operator) is that it can't be implemented in user-land code (calling it with
> a non-existing variable would cause a ReferenceError).
+1
?? has been suggested by Crockford (if you include Eich’s suggestion to check
for null, as well).
http://wiki.ecmascript.org/doku.php?id=strawman:default_operator
?? would work better than ? in JavaScript, because it wouldn’t clash with the
conditional operator in JavaScript (which CoffeeScript doesn’t need, due to its
functional if statement). Then the thing to add to the proposal would be ??
used as a unary postfix operator:
if (x??) ...
Prefix might work, too.
--
Dr. Axel Rauschmayer
[email protected]
home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss