Skip to content

Commit b27d032

Browse files
authored
Core: Fill in & warn against jQuery.proxy
Fixes jquerygh-460 Closes jquerygh-530
1 parent 1ab0e32 commit b27d032

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

src/jquery/core.js

+49
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "../disablePatches.js";
1010
var findProp,
1111
arr = [],
1212
push = arr.push,
13+
slice = arr.slice,
1314
sort = arr.sort,
1415
splice = arr.splice,
1516
class2type = {},
@@ -23,6 +24,19 @@ var findProp,
2324
// to avoid O(N^2) behavior when the engine would try matching "\s+$" at each space position.
2425
rtrim = /^[\s\uFEFF\xA0]+|([^\s\uFEFF\xA0])[\s\uFEFF\xA0]+$/g;
2526

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

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

179228
if ( jQueryVersionSince( "4.0.0" ) ) {

test/unit/jquery/core.js

+56
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,59 @@ QUnit[ jQueryVersionSince( "4.0.0" ) ? "test" : "skip" ]( "jQuery.fn.splice", fu
476476
"splice removed & added in-place" );
477477
} );
478478
} );
479+
480+
QUnit[ jQueryVersionSince( "3.3.0" ) ? "test" : "skip" ]( "jQuery.proxy", function( assert ) {
481+
assert.expect( 10 );
482+
483+
var test2, test3, test4, fn, cb,
484+
test = function() {
485+
assert.equal( this, thisObject, "Make sure that scope is set properly." );
486+
},
487+
thisObject = { foo: "bar", method: test };
488+
489+
expectWarning( assert, "jQuery.proxy", 7, function() {
490+
491+
// Make sure normal works
492+
test.call( thisObject );
493+
494+
// Basic scoping
495+
jQuery.proxy( test, thisObject )();
496+
497+
// Another take on it
498+
jQuery.proxy( thisObject, "method" )();
499+
500+
// Make sure it doesn't freak out
501+
assert.equal( jQuery.proxy( null, thisObject ), undefined,
502+
"Make sure no function was returned." );
503+
504+
// Partial application
505+
test2 = function( a ) {
506+
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
507+
};
508+
jQuery.proxy( test2, null, "pre-applied" )();
509+
510+
// Partial application w/ normal arguments
511+
test3 = function( a, b ) {
512+
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
513+
};
514+
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
515+
516+
// Test old syntax
517+
test4 = {
518+
"meth": function( a ) {
519+
assert.equal( a, "boom", "Ensure old syntax works." );
520+
}
521+
};
522+
jQuery.proxy( test4, "meth" )( "boom" );
523+
524+
// jQuery 1.9 improved currying with `this` object
525+
fn = function() {
526+
assert.equal( Array.prototype.join.call( arguments, "," ),
527+
"arg1,arg2,arg3",
528+
"args passed" );
529+
assert.equal( this.foo, "bar", "this-object passed" );
530+
};
531+
cb = jQuery.proxy( fn, null, "arg1", "arg2" );
532+
cb.call( thisObject, "arg3" );
533+
} );
534+
} );

warnings.md

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

238238
**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.
239239

240+
### \[proxy\] JQMIGRATE: jQuery.proxy() is deprecated
241+
242+
**Cause:** This method, while having some differences, is similar to native `Function.prototype.bind` so it got deprecated to promote usage of native `bind`.
243+
244+
**Solution:** Replace any calls to `jQuery.proxy(fn, context, param1, param2)` with `fn.bind(context, param1, param2)`. Be careful if you use a proxied function for event handling as jQuery matches a proxied function to its original when removing event handlers which is not the case when native `bind` is used.
245+
240246
### \[shorthand-deprecated-v3\] JQMIGRATE: jQuery.fn.click() event shorthand is deprecated
241247

242248
**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)