Bug Tracker

Ticket #1028 (closed enhancement: fixed)

Opened 1 year ago

Last modified 1 year ago

.extend() recursively

Reported by: vmx Assigned to: john
Type: enhancement Priority: major
Milestone: 1.1.4 Component: core
Version: 1.1.3 Keywords:
Cc: Needs: Review

Description (last modified by john) (diff)

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

Change History

Changed 1 year ago by john

  • type changed from bug to enhancement

Changed 1 year ago by john

  • need set to Review
  • owner set to john
  • description changed from 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]; } }}} to 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]; } }}}

Changed 1 year ago by john

  • status changed from new to closed
  • version changed from 1.1.2 to 1.1.3
  • resolution set to fixed
  • milestone changed from 1.1.3 to 1.1.4

Fixed in SVN rev [2783].

Changed 1 year ago by john

  • status changed from closed to reopened
  • resolution deleted

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

Changed 1 year ago by john

  • status changed from reopened to closed
  • resolution set to fixed

Fixed in SVN rev [2816-2817].

Note: See TracTickets for help on using tickets.