Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#7809 closed bug (duplicate)

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 );
	});
}

Change History (7)

comment:2 Changed 13 years ago by Section_Ei8ht

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.

comment:3 Changed 13 years ago by Rick Waldron

Resolution: invalid
Status: newclosed

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

comment:4 Changed 13 years ago by Section_Ei8ht

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

comment:5 Changed 13 years ago by dmethvin

Resolution: invalid
Status: closedreopened

comment:6 Changed 13 years ago by dmethvin

Resolution: duplicate
Status: reopenedclosed

comment:7 Changed 13 years ago by dmethvin

Duplicate of #6355.

Note: See TracTickets for help on using tickets.