Operators ||= and &&=
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
>>> 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.
>> 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.
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 ;-).
I'm quite partial to an undefined-or operator :)
More information about the es-discuss