Javascript中自訂事件

來源:互聯網
上載者:User
function Student(){}Student.prototype = {    sayHello:function(){this.onSayHello();},    onSayHello:function(){}    };function test() {     var stu = new Student();    stu.sayHello = function()    {        alert("ok");    }    stu.sayHello();}window.onload = test;

 

下面看看如何給事件處理常式傳遞參數:

// 將有參數的函數封裝為無參數的函數 function createFunction(obj, strFunc) {     var args = []; // 定義args 用於儲存傳遞給事件處理常式的參數     if ( ! obj)         obj = window; // 如果是全域函數則obj=window;                         //  得到傳遞給事件處理常式的參數     for ( var i = 2 ; i < arguments.length; i ++ )         args.push(arguments[i]);     // 用無參數函數封裝事件處理常式的調用     return function ()     {         obj[strFunc].apply(obj, args); // 將參數傳遞給指定的事件處理常式     } } function class1() { } class1.prototype = {         show: function () {             this .onShow();             },         onShow: function () { }     }     function objOnShow(userName) {     alert( " hello, " + userName); } function test() {     var obj = new class1();     var userName = " test " ;     obj.onShow = createFunction( null , " objOnShow " , userName);     obj.show(); } 

"因為事件機制僅傳遞一個函數的名稱,不帶有任何參數的資訊,所以無法傳遞參數進去",這是後話了,“要解決這個問題,可以從相反的思路去考慮,不考慮怎麼把參數傳進去,而是考慮如何構建一個無需參數的事件處理常式,該程式是根據有參數的事件處理常式建立的,是一個外層的封裝。”,這裡的“該程式”就是 createFunction函數,它巧妙地利用apply函數將帶參數的函數封裝為無參數函數。最後我們看看如何?自訂事件的多綁定:

// 使自訂事件支援多綁定 // 將有參數的函數封裝為無參數的函數 function createFunction(obj, strFunc) {     var args = []; // 定義args 用於儲存傳遞給事件處理常式的參數     if ( ! obj)         obj = window; // 如果是全域函數則obj=window;     // 得到傳遞給事件處理常式的參數     for ( var i = 2 ; i < arguments.length; i ++ )         args.push(arguments[i]);     // 用無參數函數封裝事件處理常式的調用     return function ()     {         obj[strFunc].apply(obj, args); // 將參數傳遞給指定的事件處理常式     } } function class1() { } class1.prototype = {     show: function () {             if ( this .onShow)             {                 for ( var i = 0 ; i < this .onShow.length; i ++ )                 {                     this .onShow[i]();                 }             }         },     attachOnShow: function (_eHandler) {             if ( ! this .onShow)                 this .onShow = [];            this .onShow.push(_eHandler);         }     } function objOnShow(userName) {     alert( " hello, " + userName); } function objOnShow2(testName) {     alert( " show: " + testName); } function test() {     var obj = new class1();     var userName = " your name " ;     obj.attachOnShow(createFunction( null , " objOnShow " , userName));     obj.attachOnShow(createFunction( null , " objOnShow2 " , " test message " ));     obj.show(); } 

 

我們看到,attachOnShow方法實現的基本思想是對數組的push操作,其實我們還可以在事件執行完成之後,移除事件處理函數,下面單獨實現:

// 將有參數的函數封裝為無參數的函數 function createFunction(obj, strFunc) {     var args = []; // 定義args 用於儲存傳遞給事件處理常式的參數     if ( ! obj)         obj = window; // 如果是全域函數則obj=window;     // 得到傳遞給事件處理常式的參數     for ( var i = 2 ; i < arguments.length; i ++ )         args.push(arguments[i]);     // 用無參數函數封裝事件處理常式的調用     return function ()     {         obj[strFunc].apply(obj, args); // 將參數傳遞給指定的事件處理常式     } } function class1() { } class1.prototype = {     show: function ()         {             if ( this .onShow)             {             for ( var i = 0 ; i < this .onShow.length; i ++ )             {                 this .onShow[i]();             }             }         },     attachOnShow: function (_eHandler)         { // 附加事件             if ( ! this .onShow)                 { this .onShow = []; }             this .onShow.push(_eHandler);         },     detachOnShow: function (_eHandler)         { // 移除事件             if ( ! this .onShow)                 { this .onShow = []; }             this .onShow.pop(_eHandler);         } } function objOnShow(userName) {     alert( " hello, " + userName); } function objOnShow2(testName) {     alert( " show: " + testName); } function test() {     var obj = new class1();     var userName = " your name " ;     obj.attachOnShow(createFunction( null , " objOnShow " , userName));     obj.attachOnShow(createFunction( null , " objOnShow2 " , " test message " ));     obj.show();     obj.detachOnShow(createFunction( null , " objOnShow " , userName));     obj.show(); // 移除一個,顯示剩餘的一個     obj.detachOnShow(createFunction( null , " objOnShow2 " , " test message " ));     obj.show(); // 兩個都移除,一個也不顯示 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.