Optional assignment
jhpratt opened this issue · 2 comments
See here for discussion on the mailing list.
Copying most of my original post for brevity:
My thought was to have the following:
this.foo ?= params?.foo;
which can be desugared to
if (($ref = params?.foo) !== undefined) { this.foo = $ref; }
I would strictly check for undefined, rather than nullish, as anything other than undefined would indicate that a value is present that can be set. If no value is present (such as a missing key on an object), nothing would be set. A reference must be used for the general case, as the object
being assigned (the RHS) could be a function or getter with side-effects.Not sure if it should be
?=
or=?
, as it would look somewhat odd (IMO) for things like?+=
or+=?
.
After looking at some prior discussion regarding ??
, my original thought could be expanded to nullish, not just undefined, but that wouldn't make as much sense to me, given the difference between the two.
👋
At the March TC39 meeting the committee decided that null-coalescing assignment was a requirement for Stage 2 advancement.
Whatever syntax/semantics the ??
operator takes will be used when adding the ??=
logical assignment operator.
@jridgewell Awesome! Did not know that. Let's hope that null coalescing will return undefined, not null, which would match this.
Feel free to close this if you'd like; I obviously wasn't aware of that decision.