I'm working with webpack and finally I can't see the opportunity of this package since it seems Object.assign make the job, but maybe there is something between the lines about this package ?
Thanks
Object. create defines properties and Object. assign only assigns them. When creating a property, assignments will create it as configurable, writable and enumerable.
Spread in object literals Note that Object.assign() can be used to mutate an object, whereas spread syntax can't. In addition, Object.assign() triggers setters on the target object, whereas spread syntax does not.
assign() pattern in Node 8.
Object.assign() The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object.
The difference between "webpack-merge" npm package and Object.assign() (or object spread) is how they handle property with the same name:
const webpackMerge = require("webpack-merge");
const object1 = {
'x': [{'a': 'a' }, { 'b': 'b' }]
}
const object2 = {
'x': [{'c': 'c' }, { 'd': 'd' }]
}
console.log('result webpackMerge: ',
webpackMerge(object1, object2)
)
console.log('result Object.assign: ',
Object.assign({}, object1, object2)
)
console.log('result Object.spread: ',
{...object1, ...object2}
)
The above will give you:
result webpackMerge: { x: [ { a: 'a' }, { b: 'b' }, { c: 'c' }, { d: 'd' } ] }
result Object.assign: { x: [ { c: 'c' }, { d: 'd' } ] }
result Object spread: { x: [ { c: 'c' }, { d: 'd' } ] }
As you can see above Object.assign() (or Object spread) overwrite the value of previous properties with the latter one, while webpack-merge concat the element of the array.
see above code in Runkit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With