Skip to content

Commit ddfd99f

Browse files
committed
Core: Fill in & warn against jQuery.proxy
Fixes gh-460
1 parent 5845951 commit ddfd99f

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/jquery/core.js

+50
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
import "../disablePatches.js";
99

1010
var findProp,
11+
arr = [],
12+
slice = arr.slice,
1113
class2type = {},
1214
oldInit = jQuery.fn.init,
1315
oldFind = jQuery.find,
@@ -19,6 +21,19 @@ var findProp,
1921
// to avoid O(N^2) behavior when the engine would try matching "\s+$" at each space position.
2022
rtrim = /^[\s\uFEFF\xA0]+|([^\s\uFEFF\xA0])[\s\uFEFF\xA0]+$/g;
2123

24+
function isFunction( obj ) {
25+
26+
// Support: Chrome <=57, Firefox <=52
27+
// In some browsers, typeof returns "function" for HTML <object> elements
28+
// (i.e., `typeof document.createElement( "object" ) === "function"`).
29+
// We don't want to classify *any* DOM node as a function.
30+
// Support: QtWeb <=3.8.5, WebKit <=534.34, wkhtmltopdf tool <=0.12.5
31+
// Plus for old WebKit, typeof returns "function" for HTML collections
32+
// (e.g., `typeof document.getElementsByTagName("div") === "function"`). (gh-4756)
33+
return typeof obj === "function" && typeof obj.nodeType !== "number" &&
34+
typeof obj.item !== "function";
35+
}
36+
2237
migratePatchFunc( jQuery.fn, "init", function( arg1 ) {
2338
var args = Array.prototype.slice.call( arguments );
2439

@@ -176,4 +191,39 @@ if ( jQueryVersionSince( "3.3.0" ) ) {
176191
}, "isWindow",
177192
"jQuery.isWindow() is deprecated"
178193
);
194+
195+
// Bind a function to a context, optionally partially applying any
196+
// arguments.
197+
// jQuery.proxy is deprecated to promote standards (specifically Function#bind)
198+
// However, it is not slated for removal any time soon
199+
migratePatchAndWarnFunc( jQuery, "proxy",
200+
function( fn, context ) {
201+
var tmp, args, proxy;
202+
203+
if ( typeof context === "string" ) {
204+
tmp = fn[ context ];
205+
context = fn;
206+
fn = tmp;
207+
}
208+
209+
// Quick check to determine if target is callable, in the spec
210+
// this throws a TypeError, but we will just return undefined.
211+
if ( !isFunction( fn ) ) {
212+
return undefined;
213+
}
214+
215+
// Simulated bind
216+
args = slice.call( arguments, 2 );
217+
proxy = function() {
218+
return fn.apply( context || this, args.concat( slice.call( arguments ) ) );
219+
};
220+
221+
// Set the guid of unique handler to the same of original handler, so it can be removed
222+
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
223+
224+
return proxy;
225+
}, "proxy",
226+
"jQuery.proxy() is deprecated"
227+
);
228+
179229
}

test/unit/jquery/core.js

+56
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,59 @@ TestManager.runIframeTest( "old pre-3.0 jQuery", "core-jquery2.html",
449449

450450
assert.ok( /jQuery 3/.test( log ), "logged: " + log );
451451
} );
452+
453+
QUnit[ jQueryVersionSince( "3.3.0" ) ? "test" : "skip" ]( "jQuery.proxy", function( assert ) {
454+
assert.expect( 10 );
455+
456+
var test2, test3, test4, fn, cb,
457+
test = function() {
458+
assert.equal( this, thisObject, "Make sure that scope is set properly." );
459+
},
460+
thisObject = { foo: "bar", method: test };
461+
462+
expectWarning( assert, "jQuery.proxy", 7, function() {
463+
464+
// Make sure normal works
465+
test.call( thisObject );
466+
467+
// Basic scoping
468+
jQuery.proxy( test, thisObject )();
469+
470+
// Another take on it
471+
jQuery.proxy( thisObject, "method" )();
472+
473+
// Make sure it doesn't freak out
474+
assert.equal( jQuery.proxy( null, thisObject ), undefined,
475+
"Make sure no function was returned." );
476+
477+
// Partial application
478+
test2 = function( a ) {
479+
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
480+
};
481+
jQuery.proxy( test2, null, "pre-applied" )();
482+
483+
// Partial application w/ normal arguments
484+
test3 = function( a, b ) {
485+
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
486+
};
487+
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
488+
489+
// Test old syntax
490+
test4 = {
491+
"meth": function( a ) {
492+
assert.equal( a, "boom", "Ensure old syntax works." );
493+
}
494+
};
495+
jQuery.proxy( test4, "meth" )( "boom" );
496+
497+
// jQuery 1.9 improved currying with `this` object
498+
fn = function() {
499+
assert.equal( Array.prototype.join.call( arguments, "," ),
500+
"arg1,arg2,arg3",
501+
"args passed" );
502+
assert.equal( this.foo, "bar", "this-object passed" );
503+
};
504+
cb = jQuery.proxy( fn, null, "arg1", "arg2" );
505+
cb.call( thisObject, "arg3" );
506+
} );
507+
} );

warnings.md

+6
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58
229229

230230
**Solution:** Remove any use of `jQuery.isWindow()` from code. If it is truly needed it can be replaced with a check for `obj != null && obj === obj.window` which was the test used inside this method.
231231

232+
### \[proxy\] JQMIGRATE: jQuery.proxy() is deprecated
233+
234+
**Cause:** `jQuery.proxy` is similar to `Function.prototype.bind`; the main difference is it preserves function identity perceived by jQuery. The most common usage of that is in event handlers - you can unbind a proxied function by passing the original one. Because of the similarity to native `bind`, `jQuery.proxy` has been deprecated in jQuery 3.3.0.
235+
236+
**Solution:** Replace any use of `jQuery.proxy()` with function `.bind()`. If a proxied function is passed as a jQuery event handler, make sure to pass the same bounded function to `.off()`.
237+
232238
### \[shorthand-deprecated-v3\] JQMIGRATE: jQuery.fn.click() event shorthand is deprecated
233239

234240
**Cause:** The `.on()` and `.trigger()` methods can set an event handler or generate an event for any event type, and should be used instead of the shortcut methods. This message also applies to the other event shorthands, including: blur, focus, focusin, focusout, resize, scroll, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress, keyup, and contextmenu.

0 commit comments

Comments
 (0)