溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

個人對JQuery Proxy()函數(shù)的理解

發(fā)布時間:2020-07-19 01:11:27 來源:網(wǎng)絡 閱讀:437 作者:weiw123 欄目:web開發(fā)

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把事件執(zhí)行內的對象改變?yōu)?/span>you。


JQuery.proxy(context,functionname):

第一個參數(shù)是你想proxy的對象,第二個參數(shù)為要改變的函數(shù)的名字。

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").

這個執(zhí)行完之后,結果會是John,

如果使用下面這句

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

結果會是$("#test").name值。


這個函數(shù)和上面的那個函數(shù)的功能一樣,就是使用了更加簡潔的方式。


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI