您的位置:首页 > Web前端 > JQuery

jquery proxy整理

2015-08-15 18:07 597 查看
看到的两篇关于jquery proxy函数不错的文章。

原文:http://www.cnblogs.com/acles/archive/2012/11/20/2779282.html

JQuery.proxy(function,context):

使用context代替function中的context。

比如:

var you = {

  type: "person",

  test: function(event) {

    $("#log").append( this.type + " " );

  }

$("#test").click(you.test);调用这句只有相当于调用:

$("#test").click(function(event){

         $("#log").append( this.type + " " );

});

所以这里的this指的是$("#test").

 

如果这样调用:$("#test").click($.proxy(you.test,you));

此时的调用相当于:

$("#test").click(function(event){

         $("#log").append( you.type + " " );

});

虽然调用事件的对象是$("#test"),但是却可以使用$.proxy把事件执行内的对象改变为you。

 

JQuery.proxy(context,functionname):

第一个参数是你想proxy的对象,第二个参数为要改变的函数的名字。

var obj = {

    name: "John",

    test: function() {

      $("#log").append( this.name );

      $("#test").unbind("click", obj.test);

    }

  };

 

  $("#test").click( jQuery.proxy( obj, "test" ) );  
把obj作为context传入test中,而不是$("#test").

这个执行完之后,结果会是John,

如果使用下面这句

$("#test").click(obj.test);

结果会是$("#test").的name值。

 

这个函数和上面的那个函数的功能一样,就是使用了更加简洁的方式。

原文:http://blog.163.com/rex_blog/blog/static/1944801012013611101725100/

jQuery.proxy(),接受一个函数,然后返回一个新函数,并且这个新函数始终保持了特定的上下文(context )语境。

jQuery.proxy( function, context )

function将要改变上下文语境的函数。
context函数的上下文语境(`this`)会被设置成这个 object 对象。

jQuery.proxy( context, name )

context函数的上下文语境会被设置成这个 object 对象。
name将要改变上下文语境的函数名(这个函数必须是前一个参数 ‘context’ 对象的属性)

这个方法通常在向一个元素上附加事件处理函数时,上下文语境实际是指向另一个对象的情况下使用。
另外,jQuery 能够确保即使你绑定的函数是经过 jQuery.proxy() 处理过的函数,你依然可以用原先的函数来正确地取消绑定。

看一下官方的例子:

var obj = {
name: "John",
test: function() {
alert( this.name );
$("#test").unbind("click", obj.test);
}
};

$("#test").click( jQuery.proxy( obj, "test" ) );

// 以下代码跟上面那句是等价的:
// $("#test").click( jQuery.proxy( obj.test, obj ) );

// 可以与单独执行下面这句做个比较。
// $("#test").click( obj.test );


可以看一下jQuery中的实现(1.6之前的版本):

/* jQuery 源码之 proxy:
使用 apply 形式, 执行回调函数.
*/
jQuery.proxy = function( fn, proxy, thisObject ) {
if ( arguments.length === 2 ) {
// jQuery.proxy(context, name);
if ( typeof proxy === "string" ) {
thisObject = fn;
fn = thisObject[ proxy ];
proxy = undefined;

/* 转化结果:
thisObject -> context
fn -> name
proxy -> undefined
*/
}
// jQuery.proxy(name, context);
else if ( proxy && !jQuery.isFunction( proxy ) ) {
thisObject = proxy;
proxy = undefined;
}
}
if ( !proxy && fn ) {
/* 使用 proxy 保证 函数执行时, context 为指定值 */
proxy = function() {
return fn.apply( thisObject || this, arguments );
};
}
// Set the guid of unique handler to the same of original handler, so it can be removed
if ( fn ) {
proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++;
}
// So proxy can be declared as an argument
return proxy;
}


其实就是平常使用的的call和apply,大部分的时候作为回调使用。

stackoverflow上有个问题,其中的例子比较典型,供参考:

比如有如下代码:

$('#myElement').click(function() {

// In this function, "this" is our DOM element.

$(this).addClass('aNewClass');

});


这里的this就是我们的DOM元素。如果我们要在增加class样式之前需要等待一段时间,可能会将代码写成下面这样(注意:有问题的代码):

$('#myElement').click(function() {

setTimeout(function() {

// Problem! In this function "this" is not our element!

$(this).addClass('aNewClass');

}, 1000);

});


这里的this就不是我们期望的那个DOM元素了。解决方法就是使用jQuery的$.proxy()了,代码如下:

$('#myElement').click(function() {

// ------------------v--------give $.proxy our function,

setTimeout($.proxy(function() {

$(this).addClass('aNewClass');  // Now "this" is again our element

}, this), 1000);

// ---^--------------and tell it that we want our DOM element to be the

//                      value of "this" in the function

});


我们可以这样来理解上面的代码:

function() {

// v--------func is the function we gave to $.proxy

func.apply( ctx );

// ----------^------ ctx is the value we wanted for "this" (our DOM element)

}


这就体现出来jQuery中$.proxy()的强大功效了。

注意:在jQuery 1.6及之后的版本中,除了上面提到的两种用法之外,proxy还增加了其他两种用法:

jQuery.proxy( function, context [, additionalArguments ] )

jQuery.proxy( context, name [, additionalArguments ] )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery 前端