Operators ||= and &&=

On May 5, 2009, at 11:37 AM, Peter Michaux wrote:
> On Mon, May 4, 2009 at 10:26 PM, Brendan Eich <brendan at mozilla.com>
> wrote:
>>> ??= and ?? are too narrowly typed for JS.
>> I had the impression from the previous thread that you were more in
> favor of ??= than ||=.
Only with an important condition. In reply to Ingvar (his words are >>
cited), I wrote:
> > Suppose you add both ||= and ??=. I think very soon a rule of
> > good practices will spread, recommending to always use ??=
> > for defaults, never ||=, because ||= can trip you up with its
> > zero/false/empty-string irregularity, and ??= is always right.
>> Then according to the principle I argue above, no intentional
> deadwood, we should add only ??= and ??. Ruby users will cope, and we
> avoid the slight difference between ||= in Ruby and ||= in ES4.
But I am not convinced using ??= is beter than ||=.
This was pulled from the thread I cited (with corrected anchor), at
https://mail.mozilla.org/pipermail/es5-discuss/2008-August/
thread.html#1704
The message the above text came from is at
https://mail.mozilla.org/pipermail/es5-discuss/2008-August/000509.html
> People often write the following at the
> beginning of functions for optional parameters
>> optionalParam = optionalParam || defaultVal;
>> I believe what they are really trying to write is
>> optionalParam = optionalParam === undefined ? defaultValue :
> optionalParam;
>> which is what ??= would do.
You could be right, but the fact is people use ||, which equates falsy
values to not short-circuit the defaultVal evaluation. This is what
the code does, not necessarily what was meant. It might be better to
pave this cowpath and not try to sell the ?? super-highway ;-).
> I think optional parameters are the nuisance use case that is fueling
> any discussion about a ||= or ??= operator.
Nuisance to you, useful to others -- and I should note, required for
self-hosting the builtins and the DOM in ES.
> I think other use cases
> are less common and probably would not warrant adding a new operator.
> The reason I mention this is if there is another sugar plan for
> default parameter values then perhaps ||= and ??= aren't needed at all
Good point, I'll add it to http://wiki.ecmascript.org/doku.php?id=harmony:default_parameter_values
(which is in harmony: because at several TC39 meetings it was
considered harmonious; this could be revisited, but for now it's in
that wiki namespace, while ||= is not).
> (though I think they are probably worth adding.)
Both?
> I thought default parameters were going to be something like the
> following.
>> function(a=1, b="defaultVal");
That's a useless expression closure :-P.
> And in this syntax will default values be used if the parameter is
> falsey or only if it is undefined?
Only if the actual parameter is absent.
/be