#9477 closed bug (wontfix)
extend (deep) merges arrays fields instead of replacing them
Reported by: | tsvetomir | Owned by: | tsvetomir |
---|---|---|---|
Priority: | low | Milestone: | 1.next |
Component: | core | Version: | 1.6.1 |
Keywords: | neededdocs | Cc: | |
Blocked by: | Blocking: |
Description (last modified by )
A deep extend does not overwrite array fields, but tries to merge them.
Example: [0, 1, 2] and [3] produce [3, 1, 2] instead of [3]. [
This behavior is not consistent or expected. If we apply it on strings "abc" and "d" should merge to "dbc".
Possibly related issue: [
Change History (13)
comment:1 Changed 12 years ago by
Component: | unfiled → core |
---|---|
Owner: | set to Rick Waldron |
Status: | new → assigned |
comment:2 Changed 12 years ago by
Owner: | changed from Rick Waldron to tsvetomir |
---|---|
Status: | assigned → pending |
comment:3 Changed 12 years ago by
Yeah, this looks correct to me. Object and Array are extended, but primitive string/boolean/number are not.
comment:4 Changed 12 years ago by
That's how I've always understood "extend" functions to behave, hopefully the OP will have references that will "enlighten" us.
comment:5 Changed 12 years ago by
Status: | pending → new |
---|
We have a defined behavior for extending objects (union), but we shouldn't assume that the user expects the same for sets of data.
Arrays should be treated as sets of data and not like objects. They can be combined in an union, intersection, a complement or in a user-defined way.
These two similar implementations yield the result I'd expect:
Object.extend (Prototype) http://jsfiddle.net/JV7m8/1/
Object.append (Mootools) http://jsfiddle.net/dThRz/1/
comment:6 Changed 12 years ago by
Priority: | undecided → low |
---|---|
Resolution: | → worksforme |
Status: | new → closed |
This yields the results exactly as shown above: http://jsfiddle.net/rwaldron/zkCZt/
comment:7 Changed 12 years ago by
extend(a, b) vs. extend(true, a, b)
You need to supply the true (deep) parameter to reproduce the issue. Otherwise you're overwriting the "nested" object http://jsfiddle.net/MN5xQ/
comment:8 Changed 12 years ago by
p.s. Turns out Prototype and Mootools don't have a true deep extend. They always overwrite nested objects and arrays. Sorry for comparing apples to oranges!
Either way, I'm not the only one confused by this behavior. See http://forum.jquery.com/topic/jquery-extend-modifies-but-not-replaces-array-properties-on-deep-copy
comment:9 Changed 12 years ago by
I just ran into this.
See: http://jsfiddle.net/s5jNF/
I would expect Arrays to be overwritten or appended. But don't replace the individual elements piecemeal, which is what happens now.
Personally I need a deep extend where the array elements are appended. (Although I can see cases where I would want them overwritten.)
Can we get this as an option?
rwaldron, please reopen this. It's not worksforsome - it's wontfix or it's open.
comment:10 Changed 12 years ago by
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
comment:11 Changed 12 years ago by
Not sure why it can't be closed worksforme with needsdocs. The proposed changes might break existing code, which would be one reason to avoid changing it. For specific needs it is no doubt faster (performance-wise) to write a custom extend method, our poor method is already overburdened with constraints.
comment:12 Changed 12 years ago by
Description: | modified (diff) |
---|---|
Keywords: | needsdocs added |
Resolution: | → wontfix |
Status: | reopened → closed |
comment:13 Changed 12 years ago by
Keywords: | neededdocs added; needsdocs removed |
---|
Can you cite references of what you believe to be the correct behaviour?