unclechu / node-deep-extend

Recursive extend module
MIT License
202 stars 53 forks source link

Arrays being modified when called with _.deepExtend #14

Closed KurtPreston closed 9 years ago

KurtPreston commented 9 years ago
a = {a: [1]}
b = {a: [2]}
c = {c: 3}
_.deepExtend.({}, a, b, c)

Expected behavior: deepExtend tries to combine the Objects, but does not modify the data in a, b, or c.

Actual behavior: deepExtend modifies the value of a.

unclechu commented 9 years ago

Correct. Actual behavior is correct. It just replace value of key, and only if values is objects both then it extends target object.

KurtPreston commented 9 years ago

Sorry, I realized my example was a bit confusing -- I was re-using variable names. This one is a little clearer:

object1 = {'key': [1]}
object2 = {'key': [2]}
_.deepExtend({}, object1, object2)

deepExtend will return the correct value (i.e. {key: [2]}). However, in doing so, it will modify the value of object1. To be consistent with underscore.js, It should only be modifying the value of the first argument.

unclechu commented 9 years ago

@KurtPreston It doesn't modify value of object1, it modify first argument object ({}). You can see it in this unit test: https://github.com/unclechu/node-deep-extend/blob/master/test/index.spec.js#L70-L79