ES6 doesn't need opt-in

Axel Rauschmayer axel at rauschma.de
Mon Jan 2 23:45:56 PST 2012


> 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
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120103/c88f99c9/attachment.html>


More information about the es-discuss mailing list