📘Object.assign()
JS ⟩ value ⟩ object ⟩ extend ⟩ Object.assign()
copies all enumerable own property values from sources to a target (with get/set).
and returns the modified target.
Object.assign(target, source1, source2, ...) // overwrite target with sources
// similar to
{...target, ...source1, ...source2}☢️ Alert:
Object.assign() copies property values (with get/set), not their attributes❗
if a source has a getter or the target has a setter, they will be invoked❗️, not copied❗️ 👉 ⛔️ Object.assign causing TypeError
use obj.mergeWith() instead if we want to copy accessors.
Object.assign()
only copies the values of enumerable properties, not their attributes❗
if one of the source objects has an accessor property, it is the value returned by the getter that is copied to the target object, not the getter itself❗
☢️ Alert:
Don't use Object.assign() with sources that have getters, the inner states of the sources may change❗❗❗ 👉 .assignDescriptors()
any
getter
TypeError
non-configurable
(new) data
configurable/writable/enumerable all true
compare with obj.mergeWith().
is used for mixin.
mergeWithoutOverride() keeps old values.
.assignDescriptors() copies descriptors instead of values.
spread operator (...) can be used instead to assign default values.
see attribute for enumerable properties.
uses [[Get]] on the source object and [[Set]] on the target object and invokes getter/setter to perform the task.
assign default values (replit)
replit ⟩ Object.assign(), require ⟩ Object extension
Last updated
Was this helpful?