Skip to content

Commit 876f847

Browse files
committed
Core: Fill in & warn against jQuery.proxy
Fixes gh-460
1 parent 95d05ce commit 876f847

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

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

185234
if ( jQueryVersionSince( "4.0.0" ) ) {

test/unit/jquery/core.js

+56
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,59 @@ QUnit[ jQueryVersionSince( "4.0.0" ) ? "test" : "skip" ]( "jQuery.fn.splice", fu
505505
"splice removed & added in-place" );
506506
} );
507507
} );
508+
509+
QUnit[ jQueryVersionSince( "3.3.0" ) ? "test" : "skip" ]( "jQuery.proxy", function( assert ) {
510+
assert.expect( 10 );
511+
512+
var test2, test3, test4, fn, cb,
513+
test = function() {
514+
assert.equal( this, thisObject, "Make sure that scope is set properly." );
515+
},
516+
thisObject = { foo: "bar", method: test };
517+
518+
expectWarning( assert, "jQuery.proxy", 7, function() {
519+
520+
// Make sure normal works
521+
test.call( thisObject );
522+
523+
// Basic scoping
524+
jQuery.proxy( test, thisObject )();
525+
526+
// Another take on it
527+
jQuery.proxy( thisObject, "method" )();
528+
529+
// Make sure it doesn't freak out
530+
assert.equal( jQuery.proxy( null, thisObject ), undefined,
531+
"Make sure no function was returned." );
532+
533+
// Partial application
534+
test2 = function( a ) {
535+
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
536+
};
537+
jQuery.proxy( test2, null, "pre-applied" )();
538+
539+
// Partial application w/ normal arguments
540+
test3 = function( a, b ) {
541+
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
542+
};
543+
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
544+
545+
// Test old syntax
546+
test4 = {
547+
"meth": function( a ) {
548+
assert.equal( a, "boom", "Ensure old syntax works." );
549+
}
550+
};
551+
jQuery.proxy( test4, "meth" )( "boom" );
552+
553+
// jQuery 1.9 improved currying with `this` object
554+
fn = function() {
555+
assert.equal( Array.prototype.join.call( arguments, "," ),
556+
"arg1,arg2,arg3",
557+
"args passed" );
558+
assert.equal( this.foo, "bar", "this-object passed" );
559+
};
560+
cb = jQuery.proxy( fn, null, "arg1", "arg2" );
561+
cb.call( thisObject, "arg3" );
562+
} );
563+
} );

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)