βπ― A bunch of these seem more clever than clean.
I consider the #1 priority of the code to easily communicate what is happening. consider these two examples
β const a = foo.x, b = foo.y;
const a = foo.x, b = foo.y;
βοΈ const { ['x']: a, ['y']: b } = foo;
const { ['x']: a, ['y']: b } = foo;
I don't know if I'm "over-indexing" on my experience here, but I'd say the "incorrect" example is easier to comprehend.
The "incorrect" version is definitely less cognitive load. I also prefer individual const definitions as well for that extra level of legibility when scanning the code.
const
#4 could also be written much simpler like this: const { x: a, y: b } = foo;
const { x: a, y: b } = foo;
But there is no b. keys are different and value is same (here a). const { ['x']: a, ['y']: a } = foo; will not be possible
const { ['x']: a, ['y']: a } = foo;
It's re-assigning the value. ['y']: b assigns the value of y to b so you can now access it as b.
['y']: b
y
b
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
βπ―
A bunch of these seem more clever than clean.
I consider the #1 priority of the code to easily communicate what is happening.
consider these two examples
β
const a = foo.x, b = foo.y;
βοΈ
const { ['x']: a, ['y']: b } = foo;
I don't know if I'm "over-indexing" on my experience here, but I'd say the "incorrect" example is easier to comprehend.
The "incorrect" version is definitely less cognitive load. I also prefer individual
const
definitions as well for that extra level of legibility when scanning the code.#4 could also be written much simpler like this:
const { x: a, y: b } = foo;
But there is no b. keys are different and value is same (here a).
const { ['x']: a, ['y']: a } = foo;
will not be possibleIt's re-assigning the value.
['y']: b
assigns the value ofy
tob
so you can now access it asb
.