Skip to main content

Bug Tracker

Side navigation

#1028 closed enhancement (fixed)

Opened March 07, 2007 09:31AM UTC

Closed August 21, 2007 04:46AM UTC

.extend() recursively

Reported by: vmx Owned by: john
Priority: major Milestone: 1.1.4
Component: core Version: 1.1.3
Keywords: Cc:
Blocked by: Blocking:
Description

extend() doesn't go through the properties recursively.

For the following case:

var a = {prop1: {prop1a: "1a", prop2a: "5a"}};
var b = {prop1: {prop1a: "1b"}};
$.extend(a, b);

you would expect the result:

a = {prop1: {prop1a: "1b", prop2a: "5a"}

but the result currently is:

a = {prop1: {prop1a: "1b"}

It copies (overwrites) the property "prop1" from "b" wihthout keeping "prop2a" from "a".

This code snipped fixes the bug (just replace the while loop in "jQuery.extend" with this code)

	while (prop = arguments[a++])
		// Extend the base object
		for ( var i in prop )
                {
                    // prevent endless inheritances/loops
                    if (target == prop[i])
                        continue;

                    // extend recursively if current prop is an object, and
                    // target has props of the same name
                    if (typeof prop[i] == 'object' && target[i])
                        jQuery.extend(target[i],prop[i]);
                    else
                        target[i] = prop[i];
                }
Attachments (0)
Change History (5)

Changed March 24, 2007 03:40AM UTC by john comment:1

type: bugenhancement

Changed August 19, 2007 06:00PM UTC by john comment:2

description: extend() doesn't go through the properties recursively.\ For the following case:\ {{{\ var a = {prop1: {prop1a: "1a", prop2a: "5a"}};\ var b = {prop1: {prop1a: "1b"}};\ $.extend(a, b);\ }}}\ you would expect the result:\ {{{\ a = {prop1: {prop1a: "1b", prop2a: "5a"}\ }}}\ but the result currently is:\ {{{\ a = {prop1: {prop1a: "1b"}\ }}}\ It copies (overwrites) the property "prop1" from "b" wihthout keeping "prop2a" from "a".\ \ This code snipped fixes the bug (just replace the while loop in "jQuery.extend" with this code)\ {{{\ while (prop = arguments[a++])\ // Extend the base object\ for ( var i in prop )\ {\ // prevent endless inheritances/loops\ if (target == prop[i])\ continue;\ \ // extend recursively if current prop is an object, and\ // target has props of the same name\ if (typeof prop[i] == 'object' && target[i])\ jQuery.extend(target[i],prop[i]);\ else\ target[i] = prop[i];\ }\ }}}\ \ \ extend() doesn't go through the properties recursively. \ For the following case: \ {{{ \ var a = {prop1: {prop1a: "1a", prop2a: "5a"}}; \ var b = {prop1: {prop1a: "1b"}}; \ $.extend(a, b); \ }}} \ you would expect the result: \ {{{ \ a = {prop1: {prop1a: "1b", prop2a: "5a"} \ }}} \ but the result currently is: \ {{{ \ a = {prop1: {prop1a: "1b"} \ }}} \ It copies (overwrites) the property "prop1" from "b" wihthout keeping "prop2a" from "a". \ \ This code snipped fixes the bug (just replace the while loop in "jQuery.extend" with this code) \ {{{ \ while (prop = arguments[a++]) \ // Extend the base object \ for ( var i in prop ) \ { \ // prevent endless inheritances/loops \ if (target == prop[i]) \ continue; \ \ // extend recursively if current prop is an object, and \ // target has props of the same name \ if (typeof prop[i] == 'object' && target[i]) \ jQuery.extend(target[i],prop[i]); \ else \ target[i] = prop[i]; \ } \ }}} \ \ \
need: → Review
owner: → john

Changed August 19, 2007 11:38PM UTC by john comment:3

milestone: 1.1.31.1.4
resolution: → fixed
status: newclosed
version: 1.1.21.1.3

Fixed in SVN rev [2783].

Changed August 20, 2007 03:30PM UTC by john comment:4

resolution: fixed
status: closedreopened

Fails violently with the dimensions plugin - I figured something like this would happen, need to make it an optional argument.

Changed August 21, 2007 04:46AM UTC by john comment:5

resolution: → fixed
status: reopenedclosed

Fixed in SVN rev [2816-2817].