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

Commit 3a8e447

Browse files
committed
Core: deprecate jQuery.proxy (not slated for removal)
Fixes gh-2958 Close gh-3885
1 parent 909e0c9 commit 3a8e447

File tree

6 files changed

+91
-87
lines changed

6 files changed

+91
-87
lines changed

src/core.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -395,35 +395,6 @@ jQuery.extend( {
395395
// A global GUID counter for objects
396396
guid: 1,
397397

398-
// Bind a function to a context, optionally partially applying any
399-
// arguments.
400-
proxy: function( fn, context ) {
401-
var tmp, args, proxy;
402-
403-
if ( typeof context === "string" ) {
404-
tmp = fn[ context ];
405-
context = fn;
406-
fn = tmp;
407-
}
408-
409-
// Quick check to determine if target is callable, in the spec
410-
// this throws a TypeError, but we will just return undefined.
411-
if ( !jQuery.isFunction( fn ) ) {
412-
return undefined;
413-
}
414-
415-
// Simulated bind
416-
args = slice.call( arguments, 2 );
417-
proxy = function() {
418-
return fn.apply( context || this, args.concat( slice.call( arguments ) ) );
419-
};
420-
421-
// Set the guid of unique handler to the same of original handler, so it can be removed
422-
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
423-
424-
return proxy;
425-
},
426-
427398
// jQuery.support is not used in Core but other projects attach their
428399
// properties to it so it needs to exist.
429400
support: support

src/deprecated.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ define( [
22
"./core",
33
"./core/nodeName",
44
"./core/camelCase",
5-
"./var/isWindow"
6-
], function( jQuery, nodeName, camelCase, isWindow ) {
5+
"./var/isWindow",
6+
"./var/slice"
7+
], function( jQuery, nodeName, camelCase, isWindow, slice ) {
78

89
"use strict";
910

@@ -28,6 +29,37 @@ jQuery.fn.extend( {
2829
}
2930
} );
3031

32+
// Bind a function to a context, optionally partially applying any
33+
// arguments.
34+
// jQuery.proxy is deprecated to promote standards (specifically Function#bind)
35+
// However, it is not slated for removal any time soon
36+
jQuery.proxy = function( fn, context ) {
37+
var tmp, args, proxy;
38+
39+
if ( typeof context === "string" ) {
40+
tmp = fn[ context ];
41+
context = fn;
42+
fn = tmp;
43+
}
44+
45+
// Quick check to determine if target is callable, in the spec
46+
// this throws a TypeError, but we will just return undefined.
47+
if ( !jQuery.isFunction( fn ) ) {
48+
return undefined;
49+
}
50+
51+
// Simulated bind
52+
args = slice.call( arguments, 2 );
53+
proxy = function() {
54+
return fn.apply( context || this, args.concat( slice.call( arguments ) ) );
55+
};
56+
57+
// Set the guid of unique handler to the same of original handler, so it can be removed
58+
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
59+
60+
return proxy;
61+
};
62+
3163
jQuery.holdReady = function( hold ) {
3264
if ( hold ) {
3365
jQuery.readyWait++;

src/effects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ function Animation( elem, properties, options ) {
387387
if ( result ) {
388388
if ( jQuery.isFunction( result.stop ) ) {
389389
jQuery._queueHooks( animation.elem, animation.opts.queue ).stop =
390-
jQuery.proxy( result.stop, result );
390+
result.stop.bind( result );
391391
}
392392
return result;
393393
}

test/unit/core.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,54 +1531,6 @@ QUnit.test( "jQuery.isEmptyObject", function( assert ) {
15311531
// equal(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
15321532
} );
15331533

1534-
QUnit.test( "jQuery.proxy", function( assert ) {
1535-
assert.expect( 9 );
1536-
1537-
var test2, test3, test4, fn, cb,
1538-
test = function() {
1539-
assert.equal( this, thisObject, "Make sure that scope is set properly." );
1540-
},
1541-
thisObject = { foo: "bar", method: test };
1542-
1543-
// Make sure normal works
1544-
test.call( thisObject );
1545-
1546-
// Basic scoping
1547-
jQuery.proxy( test, thisObject )();
1548-
1549-
// Another take on it
1550-
jQuery.proxy( thisObject, "method" )();
1551-
1552-
// Make sure it doesn't freak out
1553-
assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
1554-
1555-
// Partial application
1556-
test2 = function( a ) {
1557-
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
1558-
};
1559-
jQuery.proxy( test2, null, "pre-applied" )();
1560-
1561-
// Partial application w/ normal arguments
1562-
test3 = function( a, b ) {
1563-
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
1564-
};
1565-
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
1566-
1567-
// Test old syntax
1568-
test4 = { "meth": function( a ) {
1569-
assert.equal( a, "boom", "Ensure old syntax works." );
1570-
} };
1571-
jQuery.proxy( test4, "meth" )( "boom" );
1572-
1573-
// jQuery 1.9 improved currying with `this` object
1574-
fn = function() {
1575-
assert.equal( Array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );
1576-
assert.equal( this.foo, "bar", "this-object passed" );
1577-
};
1578-
cb = jQuery.proxy( fn, null, "arg1", "arg2" );
1579-
cb.call( thisObject, "arg3" );
1580-
} );
1581-
15821534
QUnit.test( "jQuery.parseHTML", function( assert ) {
15831535
assert.expect( 23 );
15841536

test/unit/deprecated.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,51 @@ QUnit.test( "jQuery.now", function( assert ) {
208208

209209
assert.ok( typeof jQuery.now() === "number", "jQuery.now is a function" );
210210
} );
211+
212+
QUnit.test( "jQuery.proxy", function( assert ) {
213+
assert.expect( 9 );
214+
215+
var test2, test3, test4, fn, cb,
216+
test = function() {
217+
assert.equal( this, thisObject, "Make sure that scope is set properly." );
218+
},
219+
thisObject = { foo: "bar", method: test };
220+
221+
// Make sure normal works
222+
test.call( thisObject );
223+
224+
// Basic scoping
225+
jQuery.proxy( test, thisObject )();
226+
227+
// Another take on it
228+
jQuery.proxy( thisObject, "method" )();
229+
230+
// Make sure it doesn't freak out
231+
assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
232+
233+
// Partial application
234+
test2 = function( a ) {
235+
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
236+
};
237+
jQuery.proxy( test2, null, "pre-applied" )();
238+
239+
// Partial application w/ normal arguments
240+
test3 = function( a, b ) {
241+
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
242+
};
243+
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
244+
245+
// Test old syntax
246+
test4 = { "meth": function( a ) {
247+
assert.equal( a, "boom", "Ensure old syntax works." );
248+
} };
249+
jQuery.proxy( test4, "meth" )( "boom" );
250+
251+
// jQuery 1.9 improved currying with `this` object
252+
fn = function() {
253+
assert.equal( Array.prototype.join.call( arguments, "," ), "arg1,arg2,arg3", "args passed" );
254+
assert.equal( this.foo, "bar", "this-object passed" );
255+
};
256+
cb = jQuery.proxy( fn, null, "arg1", "arg2" );
257+
cb.call( thisObject, "arg3" );
258+
} );

test/unit/event.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ QUnit.test( "on(), with different this object", function( assert ) {
694694
};
695695

696696
jQuery( "#firstp" )
697-
.on( "click", jQuery.proxy( handler1, thisObject ) ).trigger( "click" ).off( "click", handler1 )
698-
.on( "click", data, jQuery.proxy( handler2, thisObject ) ).trigger( "click" ).off( "click", handler2 );
697+
.on( "click", handler1.bind( thisObject ) ).trigger( "click" ).off( "click", handler1 )
698+
.on( "click", data, handler2.bind( thisObject ) ).trigger( "click" ).off( "click", handler2 );
699699

700700
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using different this object and data." );
701701
} );
@@ -1640,18 +1640,19 @@ QUnit.test( ".on()/.off()", function( assert ) {
16401640
jQuery( "#body" ).off( "click", "#foo" );
16411641

16421642
// Test binding with different this object
1643-
jQuery( "#body" ).on( "click", "#foo", jQuery.proxy( function() {
1644-
assert.equal( this.foo, "bar", "on with event scope" ); }, { "foo": "bar" }
1645-
) );
1643+
jQuery( "#body" ).on( "click", "#foo", function() {
1644+
assert.equal( this.foo, "bar", "on with event scope" );
1645+
}.bind( { "foo": "bar" } ) );
1646+
16461647
jQuery( "#foo" ).trigger( "click" );
16471648
jQuery( "#body" ).off( "click", "#foo" );
16481649

16491650
// Test binding with different this object, event data, and trigger data
1650-
jQuery( "#body" ).on( "click", "#foo", true, jQuery.proxy( function( e, data ) {
1651+
jQuery( "#body" ).on( "click", "#foo", true, function( e, data ) {
16511652
assert.equal( e.data, true, "on with with different this object, event data, and trigger data" );
16521653
assert.equal( this.foo, "bar", "on with with different this object, event data, and trigger data" );
16531654
assert.equal( data, true, "on with with different this object, event data, and trigger data" );
1654-
}, { "foo": "bar" } ) );
1655+
}.bind( { "foo": "bar" } ) );
16551656
jQuery( "#foo" ).trigger( "click", true );
16561657
jQuery( "#body" ).off( "click", "#foo" );
16571658

0 commit comments

Comments
 (0)