Javascript has many shorthands for assignment operator like ??=
, &&=
, +=
and others. Also objects can be initialized using literal notation: const a = 5; const obj = { a };
. So how about a shorthand for property assignment which based on them?
It's syntax is very simple. Instead of:
obj.prop = prop
We just write
obj .= prop
The rule is very simple. Shorthand is allowed on object property assignment to variable when property and variable names are equal. The syntax is intuitive and clear, IMO
So, what do you think? Please share your opinion.
I disagree on that particular point.
In general, obj <OP>= val
is roughly equivalent to obj = obj <OP> val
; e.g.
-
obj += val
is equivalent toobj = obj + val
, except thatobj
is evaluated only once; -
obj ??= val
is equivalent toobj = obj ?? val
, except that assignment may be skipped per short-circuiting semantics.
So I expect obj .= prop
be equivalent to obj = obj.prop
, which is completely different from you mean.
1 Like
And this potential point of confusion makes me feel this is probably not a good idea to add.