Default operator strawman - ||| rather than ??

Tab Atkins Jr. jackalmage at gmail.com
Tue Jun 12 18:11:48 PDT 2012


On Tue, Jun 12, 2012 at 2:52 PM, Thaddee Tyl <thaddee.tyl at gmail.com> wrote:
> On the other hand, non-existent properties are a use-case.
> CoffeeScript provides a similar feature, but for null properties.
>
> For example, the following:
>
>    o =
>     a: 1
>     b: 2
>
>    alert o.c?.d
>
> compiles to:
>
>    var o, _ref;
>
>    o = {
>      a: 1,
>      b: 2
>    };
>
>    alert((_ref = o.c) != null ? _ref.d : void 0);
>
> Special-casing undefined makes as much sense as special-casing null,
> like CoffeeScript does.
>
> My point is that it is not obvious whether the non-existent properties
> use-case should be "undefined"-specific or "null"-specific (or both).

That's... pretty wrong.  In the example above, o.c returns undefined,
not null.  If CoffeeScript *actually* compiles to the code you posted,
then it only works because they're using != instead of !==, and null
and undefined are both falsey.  However, it'll break if o.c is defined
and is a falsey value like 0 or false.

Try it.  Put the following in your nearest console and see what it returns:

({a:1, b:2}).c === undefined

~TJ


More information about the es-discuss mailing list