Bug Tracker

Modify

Ticket #6177 (closed enhancement: wontfix)

Opened 3 years ago

Last modified 3 years ago

Enhance jQuery.extend() to allow option for each object to be a target respectively.

Reported by: patrickwhalen Owned by:
Priority: Milestone: 1.4.4
Component: core Version: 1.4.1
Keywords: extend() inheritance Cc:
Blocking: Blocked by:

Description

Considering the following example:

a = {a: 'a'};

b = {b: 'b'};

c = {c: 'c'};

jQuery.extend(a, b, c);

...the result will be:

a = {a: 'a', b: 'b', c: 'c'};

b = {b: 'b'};

c = {c: 'c'};

...which is (and ought to be) the default behavior.

The enhancement would allow a flag to be set which modifies each object with the content of the ones which follow it, creating a true inheritance chain.

So, considering the modified example:

a = {a: 'a'};

b = {b: 'b'};

c = {c: 'c'};

jQuery.extend(a, b, c, true);

...the new result would be:

a = {a: 'a', b: 'b', c: 'c'};

b = {b: 'b', c: 'c'};

c = {c: 'c'};

...effectively doing something like:

var array = [a,b,c];

for(var i = (array.length - 2); i >= 0; i--) {

Query.extend(array[i], array[i+1]);

}

If the 'deep' parameter is set, it would of course need to set that parameter on the extend() in the 'for' loop.

Change History

comment:1 follow-up: ↓ 2 Changed 3 years ago by john

  • Status changed from new to closed
  • Resolution set to wontfix
  • Component changed from unfiled to core

Hmm, I disagree that this should be an option. It seems like it's easy enough to duplicate:

jQuery.extend( a, jQuery.extend( b, c ) )

And this way there isn't any unintended (or unexpected) duplication.

comment:2 in reply to: ↑ 1 Changed 3 years ago by patrickwhalen

Replying to john:

Hmm, I disagree that this should be an option. It seems like it's easy enough to duplicate:

jQuery.extend( a, jQuery.extend( b, c ) )

And this way there isn't any unintended (or unexpected) duplication.

That method works on my simple example because we know in advance how many objects there will be. Even if you did know, it would become unwieldy if there were too many more than 3 or 4.

The 'for' loop method seems to work just fine. Just thought it would make a nice shortcut to have the optional functionality. Not a big deal though, especially if you are concerned with cluttering the API with utilities that my not get too much use.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.