[英]What is the difference between Webpack-merge and Object.assign()?
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 ? 我正在使用webpack,最后我看不到这个包的机会,因为似乎Object.assign完成了这项工作,但也许这个包之间有什么东西?
Thanks 谢谢
The difference between "webpack-merge" npm package and Object.assign() (or object spread) is how they handle property with the same name: “webpack-merge”npm包和Object.assign()(或对象扩展)之间的区别在于它们如何处理具有相同名称的属性:
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. 正如您在上面所看到的,Object.assign()(或Object spread)用后一个属性覆盖以前属性的值,而webpack-merge连接数组的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.