Bug Tracker

Opened 5 years ago

Closed 5 years ago

#14838 closed bug (notabug)

Extend maintains reference to jQuery objects

Reported by: Mottie Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.11.0-rc1
Keywords: Cc:
Blocked by: Blocking:

Description

When extending a simple object which contains a jQuery object, the copy maintains a reference to the original object (demo).

var obj = {
        test: $('#test1')
    },
    copy = $.extend(true, {}, obj);
// copy maintains reference to obj
copy.test.addClass('red');

To get around this issue, I had to specifically clone the jQuery object, instead of extending the object.

var obj = {
        test: $('#test2')
    },
    copy = {};
// reference is not maintained
copy.test = obj.test.clone().addClass('red');

I read through the documentation and found this note on object wrappers

On a deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not.

If this does apply to this case, and this need falls outside of the extend function's behavior, then I guess I am not understanding the wording of the above statement.

This issue occurs in all versions of jQuery, all the way back to v1.2.6.

Change History (1)

comment:1 Changed 5 years ago by Rick Waldron

Resolution: notabug
Status: newclosed

This is intended behavior, see explanation here: http://jsfiddle.net/rwaldron/5s7Nk/

Note: See TracTickets for help on using tickets.