jquery工具方法proxy

时间:2023-03-10 00:36:21
jquery工具方法proxy

proxy : 改变this指向

使用方法1:
function show(){

  alert(this);

}
$.proxy(show,document)();  //document

使用方法2:
function show(n1,n2){

  alert(n1);
  alert(n2);

  alert(this);

}
$.proxy(show,document)(3,4);  //document  3  4
$.proxy(show,document,3,4)();  //document  3  4
$.proxy(show,document,3)(4);  //document  3  4

使用方法3:
var obj = {

  show:function(){

    alert(this);

  }

};

$(document).click($.proxy(obj,'show')); //object

从proxy方法的源码可以看出,以下代码片段是针对 ' 使用方法3 ' 做了处理,它允许第二个参数为字符串,其实等同于:$(document).click($.proxy(obj.show,obj));

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

以下代码片段是针对传参的处理,先使用core_slice.call截取第3个参数到最后,再获取调用时的参数,最后合并。

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

proxy完整的代码:

var core_slice = Array.prototype.slice,

    ....................

jQuery.extend({

    ......................

    // A global GUID counter for objects
guid: 1, // Bind a function to a context, optionally partially applying any
// arguments.
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 ( !jQuery.isFunction( fn ) ) {
return undefined;
} // Simulated bind
args = core_slice.call( arguments, 2 );
proxy = function() {
return fn.apply( context, args.concat( core_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;
}, ............... });