Skip to content

Commit

Permalink
Core: Fill in & warn against jQuery.proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
mgol committed Sep 16, 2024
1 parent 5845951 commit d982747
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/jquery/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import "../disablePatches.js";

var findProp,
arr = [],
slice = arr.slice,
class2type = {},
oldInit = jQuery.fn.init,
oldFind = jQuery.find,
Expand Down Expand Up @@ -176,4 +178,39 @@ if ( jQueryVersionSince( "3.3.0" ) ) {
}, "isWindow",
"jQuery.isWindow() is deprecated"
);

// Bind a function to a context, optionally partially applying any
// arguments.
// jQuery.proxy is deprecated to promote standards (specifically Function#bind)
// However, it is not slated for removal any time soon
migratePatchAndWarnFunc( jQuery, "proxy",
function( fn, context ) {
var tmp, args, proxy;

if ( typeof context === "string" ) {
tmp = fn[ context ];
context = fn;
fn = tmp;
}

// Quick check to determine if target is callable, in the spec
// this throws a TypeError, but we will just return undefined.
if ( typeof fn !== "function" ) {
return undefined;
}

// Simulated bind
args = slice.call( arguments, 2 );
proxy = function() {
return fn.apply( context || this, args.concat( slice.call( arguments ) ) );
};

// Set the guid of unique handler to the same of original handler, so it can be removed
proxy.guid = fn.guid = fn.guid || jQuery.guid++;

return proxy;
}, "proxy",
"jQuery.proxy() is deprecated"
);

}
56 changes: 56 additions & 0 deletions test/unit/jquery/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,59 @@ TestManager.runIframeTest( "old pre-3.0 jQuery", "core-jquery2.html",

assert.ok( /jQuery 3/.test( log ), "logged: " + log );
} );

QUnit[ jQueryVersionSince( "3.3.0" ) ? "test" : "skip" ]( "jQuery.proxy", function( assert ) {
assert.expect( 10 );

var test2, test3, test4, fn, cb,
test = function() {
assert.equal( this, thisObject, "Make sure that scope is set properly." );
},
thisObject = { foo: "bar", method: test };

expectWarning( assert, "jQuery.proxy", 7, function() {

// Make sure normal works
test.call( thisObject );

// Basic scoping
jQuery.proxy( test, thisObject )();

// Another take on it
jQuery.proxy( thisObject, "method" )();

// Make sure it doesn't freak out
assert.equal( jQuery.proxy( null, thisObject ), undefined,
"Make sure no function was returned." );

// Partial application
test2 = function( a ) {
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
};
jQuery.proxy( test2, null, "pre-applied" )();

// Partial application w/ normal arguments
test3 = function( a, b ) {
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
};
jQuery.proxy( test3, null, "pre-applied" )( "normal" );

// Test old syntax
test4 = {
"meth": function( a ) {
assert.equal( a, "boom", "Ensure old syntax works." );
}
};
jQuery.proxy( test4, "meth" )( "boom" );

// jQuery 1.9 improved currying with `this` object
fn = function() {
assert.equal( Array.prototype.join.call( arguments, "," ),
"arg1,arg2,arg3",
"args passed" );
assert.equal( this.foo, "bar", "this-object passed" );
};
cb = jQuery.proxy( fn, null, "arg1", "arg2" );
cb.call( thisObject, "arg3" );
} );
} );
6 changes: 6 additions & 0 deletions warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ See jQuery-ui [commit](https://github.com/jquery/jquery-ui/commit/c0093b599fcd58

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

### \[proxy\] JQMIGRATE: jQuery.proxy() is deprecated

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

**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()`.

### \[shorthand-deprecated-v3\] JQMIGRATE: jQuery.fn.click() event shorthand is deprecated

**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.
Expand Down

0 comments on commit d982747

Please sign in to comment.