Ticket #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: | ||
| Blocking: | Blocked by: |
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
comment:2 Changed 2 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 2 years ago by rwaldron
- Status changed from new to closed
- Resolution set to invalid
If you augment or extend native/host objects, such as Array.prototype, your code will exhibit unexpected behaviour
comment:4 Changed 2 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 2 years ago by dmethvin
- Status changed from closed to reopened
- Resolution invalid deleted
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

http://jsfiddle.net/ambar/qQkYA/