Skip to main content

Bug Tracker

Side navigation

#7809 closed bug (duplicate)

Opened December 20, 2010 06:53AM UTC

Closed December 21, 2010 02:57AM UTC

Last modified December 21, 2010 02:57AM UTC

jQuery Clone Bug 1.4.2+

Reported by: ambar Owned by:
Priority: undecided Milestone: 1.6
Component: unfiled Version: 1.4.4
Keywords: Cc:
Blocked by: Blocking:
Description

code

Array.prototype.xyzz = function (arg) {
	console.log(1,this,arg);
};
Array.prototype.xyzzz = function (arg) {
	console.log(2,this,arg);
};

$(function() {
	$('button').click(function () {
		$('div.demo').clone(true).appendTo( 'body' );
	})
	$('div.demo').click(function () {
		console.log('click..');
	})    
});

// click the new div will trigger this bug.

bug src

// event.js, jQuery.event.add:
// before jQuery 1.4.1
handlers = events[ type ] = {};
// jQuery 1.4.2+
handlers = events[ type ] = [];

// manipulation.js, jQuery.clone : , cloneCopyEvent():
for ( var type in events ) {
	for ( var handler in events[ type ] ) {
		jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
	}
}

resolve

there is no hasOwnProperty() check in for...in loop, use each() is better.

var self = this;
for ( var type in events ) {
	jQuery.each(events[ type ],function(idx,evt) {
		jQuery.event.add( self, type, evt.handler, evt.data );
	});
}
Attachments (0)
Change History (7)

Changed December 20, 2010 07:04AM UTC by ambar comment:1

Changed December 20, 2010 10:15PM UTC by Section_Ei8ht comment:2

Came here to submit this exact bug. Could be solved using either each() or a for statement rather than a for-in statement as the nested loop.

Changed December 20, 2010 10:34PM UTC by rwaldron comment:3

resolution: → invalid
status: newclosed

If you augment or extend native/host objects, such as Array.prototype, your code will exhibit unexpected behaviour

Changed December 21, 2010 01:48AM UTC by Section_Ei8ht comment:4

I apologize if this comment comes across as harsh, but I disagree with the reason for invalidating this ticket. jQuery has a no conflict mode to allow it to be used together with libraries like Prototype, but Prototype modifies the prototypes of several native objects (Array, in this case), which makes this a bug in the compatibility of jQuery with other libraries. You shouldn't say that the fix for this problem is to simply not do it when your documentation says that you can.

It's a one line change, and I respectfully feel that the reason supplied for closing this ticket seems to me to be against the entire mantra of jQuery itself (everything being contained in the jQuery namespace), and a cop-out answer.

I've submitted a pull request to the jQuery github repository that fixes this ticket: https://github.com/jquery/jquery/pull/138

Changed December 21, 2010 02:57AM UTC by dmethvin comment:5

resolution: invalid
status: closedreopened

Changed December 21, 2010 02:57AM UTC by dmethvin comment:6

resolution: → duplicate
status: reopenedclosed

Changed December 21, 2010 02:57AM UTC by dmethvin comment:7

Duplicate of #6355.