Default operator strawman - ||| rather than ??

Thaddee Tyl thaddee.tyl at gmail.com
Tue Jun 12 15:14:39 PDT 2012


On Tue, Jun 12, 2012 at 1:56 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:
> On Tue, Jun 12, 2012 at 1:37 PM, Peter van der Zee <ecma at qfox.nl> wrote:
>> On Tue, Jun 12, 2012 at 5:29 PM, T.J. Crowder <tj at crowdersoftware.com> wrote:
>>> In the current default operator strawman[1], the operator is ??, e.g.:
>>>
>>> a = b ?? 5;
>>>
>>> is shorthand for
>>>
>>> a = b !== undefined ? b : 5;
>>
>> I missed this discussion. What validates the introduction of this
>> syntax over the equally simple and already possible `a = b || 5`? Is
>> the comparison to `undefined` (and why not `==null` as well??) really
>> worth the introduction (and subsequent loss of future usage) of the
>> double question mark? Whatever it's usual name is (double wat?).
>
> If b is falsey (0, false, null), it'll use the 5, even though you only
> intended it to be used when b is undefined.
>
> "undefined" is special-cased here because it's an extremely common
> value to check against.  It's used when an argument isn't supplied, or
> when you try to pull a non-existent property off of an object.

A non-supplied argument is a use-case that is already covered by
default parameters.

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).


More information about the es-discuss mailing list