Skip to content

Commit

Permalink
Core: Fill in & warn against jQuery.proxy
Browse files Browse the repository at this point in the history
Fixes gh-460
  • Loading branch information
mgol committed Sep 16, 2024
1 parent 5845951 commit 95b1ea0
Show file tree
Hide file tree
Showing 2 changed files with 93 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" );
} );
} );

0 comments on commit 95b1ea0

Please sign in to comment.