🌐 AI搜索 & 代理 主页
Skip to content

Commit 66975de

Browse files
committed
Remove the .bind(name, fn, thisObject) and promote jQuery.event.proxy() to jQuery.proxy() as alternative to handling scoping on callbacks. Fixes #5736.
1 parent a00e63e commit 66975de

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

src/event.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jQuery.event = {
3535
var fn = handler;
3636

3737
// Create unique handler function, wrapped around original handler
38-
handler = this.proxy( fn );
38+
handler = jQuery.proxy( fn );
3939

4040
// Store data in unique handler
4141
handler.data = data;
@@ -405,24 +405,6 @@ jQuery.event = {
405405
return event;
406406
},
407407

408-
proxy: function( fn, proxy, thisObject ) {
409-
if ( proxy !== undefined && !jQuery.isFunction( proxy ) ) {
410-
thisObject = proxy;
411-
proxy = undefined;
412-
}
413-
414-
// FIXME: Should proxy be redefined to be applied with thisObject if defined?
415-
proxy = proxy || function() {
416-
return fn.apply( thisObject !== undefined ? thisObject : this, arguments );
417-
};
418-
419-
// Set the guid of unique handler to the same of original handler, so it can be removed
420-
proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
421-
422-
// So proxy can be declared as an argument
423-
return proxy;
424-
},
425-
426408
special: {
427409
ready: {
428410
// Make sure the ready event is setup
@@ -474,6 +456,24 @@ jQuery.event = {
474456
}
475457
};
476458

459+
jQuery.event.proxy = jQuery.proxy = function( fn, proxy, thisObject ) {
460+
if ( proxy !== undefined && !jQuery.isFunction( proxy ) ) {
461+
thisObject = proxy;
462+
proxy = undefined;
463+
}
464+
465+
// FIXME: Should proxy be redefined to be applied with thisObject if defined?
466+
proxy = proxy || function() {
467+
return fn.apply( thisObject !== undefined ? thisObject : this, arguments );
468+
};
469+
470+
// Set the guid of unique handler to the same of original handler, so it can be removed
471+
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.event.guid++;
472+
473+
// So proxy can be declared as an argument
474+
return proxy;
475+
};
476+
477477
jQuery.Event = function( src ) {
478478
// Allow instantiation without the 'new' keyword
479479
if ( !this.preventDefault ) {
@@ -759,7 +759,7 @@ if ( document.addEventListener ) {
759759
}
760760

761761
jQuery.each(["bind", "one"], function( i, name ) {
762-
jQuery.fn[ name ] = function( type, data, fn, thisObject ) {
762+
jQuery.fn[ name ] = function( type, data, fn ) {
763763
// Handle object literals
764764
if ( typeof type === "object" ) {
765765
for ( var key in type ) {
@@ -773,12 +773,13 @@ jQuery.each(["bind", "one"], function( i, name ) {
773773
fn = data;
774774
data = undefined;
775775
}
776-
fn = thisObject === undefined ? fn : jQuery.event.proxy( fn, thisObject );
777-
var handler = name === "one" ? jQuery.event.proxy( fn, function( event ) {
776+
777+
var handler = name === "one" ? jQuery.proxy( fn, function( event ) {
778778
jQuery( this ).unbind( event, handler );
779779
return fn.apply( this, arguments );
780780
}) : fn;
781-
return type === "unload" ? this.one(type, data, handler, thisObject) : this.each(function() {
781+
782+
return type === "unload" ? this.one(type, data, handler) : this.each(function() {
782783
jQuery.event.add( this, type, handler, data );
783784
});
784785
};
@@ -820,10 +821,10 @@ jQuery.fn.extend({
820821

821822
// link all the functions, so any of them can unbind this click handler
822823
while ( i < args.length ) {
823-
jQuery.event.proxy( fn, args[ i++ ] );
824+
jQuery.proxy( fn, args[ i++ ] );
824825
}
825826

826-
return this.click( jQuery.event.proxy( fn, function( event ) {
827+
return this.click( jQuery.proxy( fn, function( event ) {
827828
// Figure out which function to execute
828829
var lastToggle = ( jQuery.data( this, "lastToggle" + fn.guid ) || 0 ) % i;
829830
jQuery.data( this, "lastToggle" + fn.guid, lastToggle + 1 );
@@ -840,17 +841,16 @@ jQuery.fn.extend({
840841
return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver );
841842
},
842843

843-
live: function( type, data, fn, thisObject ) {
844+
live: function( type, data, fn ) {
844845
if ( jQuery.isFunction( data ) ) {
845-
if ( fn !== undefined ) {
846-
thisObject = fn;
847-
}
848846
fn = data;
849847
data = undefined;
850848
}
849+
851850
jQuery( this.context ).bind( liveConvert( type, this.selector ), {
852851
data: data, selector: this.selector, live: type
853-
}, fn, thisObject );
852+
}, fn );
853+
854854
return this;
855855
},
856856

test/unit/event.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ test("bind(), with different this object", function() {
232232
};
233233

234234
jQuery("#firstp")
235-
.bind("click", handler1, thisObject).click().unbind("click", handler1)
236-
.bind("click", data, handler2, thisObject).click().unbind("click", handler2);
235+
.bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
236+
.bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
237237

238238
ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
239239
});
@@ -706,15 +706,15 @@ test(".live()/.die()", function() {
706706
jQuery("#foo").trigger("click", true).die("click");
707707

708708
// Test binding with different this object
709-
jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
709+
jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
710710
jQuery("#foo").trigger("click").die("click");
711711

712712
// Test binding with different this object, event data, and trigger data
713-
jQuery("#foo").live("click", true, function(e, data){
713+
jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
714714
equals( e.data, true, "live with with different this object, event data, and trigger data" );
715715
equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
716716
equals( data, true, "live with with different this object, event data, and trigger data")
717-
}, { foo: "bar" });
717+
}, { foo: "bar" }));
718718
jQuery("#foo").trigger("click", true).die("click");
719719

720720
// Verify that return false prevents default action

0 commit comments

Comments
 (0)