admin管理员组文章数量:1292937
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
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
Share Improve this question edited Jul 16, 2018 at 16:11 apollo 2,8171 gold badge24 silver badges35 bronze badges asked Jul 6, 2018 at 3:42 Diagathe JosuéDiagathe Josué 12.1k14 gold badges49 silver badges93 bronze badges 1-
2
Object.assign()
does not do the same thing.merge()
will concatenate arrays instead of clobbering indices with the values in the nearest to last array-like argument.merge()
's behavior is also highly customizable, unlikeObject.assign()
. – Patrick Roberts Commented Jul 6, 2018 at 4:07
1 Answer
Reset to default 15The 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
本文标签: javascriptWhat is the difference between Webpackmerge and Objectassign()Stack Overflow
版权声明:本文标题:javascript - What is the difference between Webpack-merge and Object.assign()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741567253a2385800.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论