Operators ||= and &&=

Ash Berlin ash_es4 at firemirror.com
Wed May 6 14:53:48 PDT 2009


On 6 May 2009, at 22:38, Brendan Eich wrote:

> On May 6, 2009, at 1:43 PM, Ash Berlin wrote:
>
>>> I don't think ||= and ??= are very difficult to define clearly.
>>> Perhaps just a line each in terms of the expanded syntax. I don't
>>> think they would add much bloat to engines. Perhaps just better to  
>>> add
>>> them both and move on to discussing classes, lambdas, or processes.
>>>
>>
>> a ||= b;
>> a = a || b;
>
> Is the reference to |a| computed only once, and before b is  
> evaluated? This matters in JS since evaluating b could change where | 
> a| exists on the scope chain.

yes.

>
>
>> a ??= b;
>> a = a === undefined ? b : a;
>
> In no case should |a| be evaluated twice here, but this desugaring  
> does that.
>
> Nothing's ever as simple as you want :-P.

Good point.

a ??= b;
if (a === undefined)  a = b;

Or if you prefer macro style

var a = (a_val);
if (a === undefined) a = (b);

>
>
>> You could make a case for adding a ?? operator for symmetry.  
>> Example of usage:
>>
>> some_func(a, b ?? "c" );
>
> Roger that, given ??= it would be strange to leave out ??.
>
> I'm still trying to leave out both ;-).
>
> /be

I'm quite partial to an undefined-or operator :)

-ash





More information about the es-discuss mailing list