Skip to content

Commit 95b1ea0

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

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/jquery/core.js

Lines changed: 37 additions & 0 deletions
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,
@@ -176,4 +178,39 @@ if ( jQueryVersionSince( "3.3.0" ) ) {
176178
}, "isWindow",
177179
"jQuery.isWindow() is deprecated"
178180
);
181+
182+
// Bind a function to a context, optionally partially applying any
183+
// arguments.
184+
// jQuery.proxy is deprecated to promote standards (specifically Function#bind)
185+
// However, it is not slated for removal any time soon
186+
migratePatchAndWarnFunc( jQuery, "proxy",
187+
function( fn, context ) {
188+
var tmp, args, proxy;
189+
190+
if ( typeof context === "string" ) {
191+
tmp = fn[ context ];
192+
context = fn;
193+
fn = tmp;
194+
}
195+
196+
// Quick check to determine if target is callable, in the spec
197+
// this throws a TypeError, but we will just return undefined.
198+
if ( typeof fn !== "function" ) {
199+
return undefined;
200+
}
201+
202+
// Simulated bind
203+
args = slice.call( arguments, 2 );
204+
proxy = function() {
205+
return fn.apply( context || this, args.concat( slice.call( arguments ) ) );
206+
};
207+
208+
// Set the guid of unique handler to the same of original handler, so it can be removed
209+
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
210+
211+
return proxy;
212+
}, "proxy",
213+
"jQuery.proxy() is deprecated"
214+
);
215+
179216
}

test/unit/jquery/core.js

Lines changed: 56 additions & 0 deletions
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+
} );

0 commit comments

Comments
 (0)