Should .offset() make a defensive copy of the position object before passing it to a callback?
I recently tried to write code like this:
$("h1").offset(function(index,curpos) {
curpos.left += 25*index;
return curpos;
});
It doesn't work, if I do it that way. Instead I have to write:
$("h1").offset(function(index,curpos) {
return {left: curpos.left + 25*index, top:curpos.top};
});
jQuery either needs to make a defensive copy before calling the callback or the documentation needs to clearly state that the callback isn't allowed to modify the passed object. I prefer the former and propose the patch below.
David Flanagan
- --- jquery-1.4.2.js 2010-05-18 10:37:30.000000000 -0700
- +++ jquery-1.4.2.patch.js 2010-08-31 16:50:32.000000000 -0700
- @@ -6071,7 +6071,7 @@
- curLeft = parseInt( jQuery.curCSS( elem, "left", true ), 10 ) || 0;
-
- if ( jQuery.isFunction( options ) ) {
- - options = options.call( elem, i, curOffset );
- + options = options.call( elem, i, {left:curOffset.left,top:curOffset.top} );
- }
-
- var props = {