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值。


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