Operators ||= and &&=
Brendan Eich
brendan at mozilla.com
Wed May 6 12:42:35 PDT 2009
On May 6, 2009, at 11:07 AM, Peter Michaux wrote:
> A lot of people don't understand JavaScript falsey value equality and
> since || works in their server-side language they assume it works the
> same way in JavaScript. It compiles and runs after all! ;-)
Compiling and running is often all that's ever needed -- so it ain't
"broke."
> A small marketing campaign to encourage the ?: version above might
> change what people use.
I doubt that. No budget, or even: no developer on scene still. For new
code, maybe, but you will hear from many devs who do not want to be
pedantic and use === and ?:. They will argue they know what they're
doing, the code works even for falsy values which are never passed and
covered by tests, and so on. Again, it ain't broke.
> I think I was unclear. Optional parameters are definitely useful. It
> is the following idiom's verbosity that is the nuisance (to be
> possibly alleviated by a new operator.)
>
> optionalParam = optionalParam || defaultValue;
Gotcha -- still it beats an if statement. The operator precedent from
Ruby (Ruby's ||= is a bit different as discussed in the past) looms
over this thread, though, so let's try to deal with it:
> 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.
I think it's better to leave both out, or add only one, than to add
both. Otherwise too much is added, and we can't easily take anything
out.
In this I think TC39 is agreed, and I'm shoulder to shoulder with Doug
and Mark on this point. I may differ on wanting to add things they
don't see the need for, but I do not want to add "both" or
"everything" just to cater to divergent opinions about how to provide
something like default parameter values.
Current position: long default parameter values, short ||= and ??=.
>> Only if the actual parameter is absent.
>
> Well that is more useful than the ||= or ??= idioms at the top of the
> function body anyway. Someone passing the value "undefined" to a
> function causes problems for the idioms.
Indeed!
/be
More information about the es-discuss
mailing list