-
Notifications
You must be signed in to change notification settings - Fork 20
/
jquery.proxy.js
43 lines (35 loc) · 1.04 KB
/
jquery.proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
jQuery.proxy extracted from the jQuery source
Here's a demo: http://jsfiddle.net/BuZ7J/
Credits: @coutoantisocial
Usage:
// Extend
var obj = proxy(function () {
return this;
}, document);
obj() // returns document
*/
(function ( window ) {
// Shortcuts
var arrProto = Array.prototype,
slice = arrProto.slice,
toString = arrProto.toString;
window.proxy = function( fn, context ) {
if ( typeof context === "string" ) {
var 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 (toString.call( fn ) !== '[object Function]') {
return undefined;
}
// Simulated bind
var args = slice.call( arguments, 2 ),
proxy = function() {
return fn.apply( context, args.concat( slice.call( arguments) ) );
};
return proxy;
}
}( window ));