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, unlike Object.assign(). – Patrick Roberts Commented Jul 6, 2018 at 4:07
Add a ment  | 

1 Answer 1

Reset to default 15

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

本文标签: javascriptWhat is the difference between Webpackmerge and Objectassign()Stack Overflow