DEV Community

Discussion on: ES 2021 Features

Collapse
 
hankyjanky profile image
Hanky Janky

I don't really understand number 4

Collapse
 
dylanoshima profile image
Dylan

From the proposal I believe a ||= b is syntactic sugar for:
a = a || b, but is meant to not trigger the object's setter function (if it has one).

They use the example: obj.a = obj.a ?? b would trigger the setter for obj.a since it's being reassigned in both instances. To prevent this we could do obj.a ?? (obj.a = b); which would only trigger the reassignment if obj.a == null, but apparently that's weird. Thus to make it succinct they propose:
a ??= b.
Which will not trigger the setter unless it needs to.

Collapse
 
hankyjanky profile image
Hanky Janky

Thanks!

Collapse
 
jsdev profile image
Andy Li

It's just like an assignment, except that it will only get assigned if a is already false. If it isn't, then no assignment, a will still be what it was.