While working on Stencil, i discovered that Javascript’s assignment operator returns the assigned value. This behaviour allows us to reuse the result of the a function multiple times within a single line of code.
const slotName = (newChild['s-sn'] = getSlotName(newChild));
You can view the whole code in GitHub
In this code, we assign the return value of getSlotName
to slotName
and 's-sn'
property of newChild
.
After writing JS for few years, i almost forgot this thing and never used it before.
Is it good for readability, dx, efficiency? I am not sure… but it is fun to discover a little aspect of assignment operator.
Official Docs