學習JavaScript設計模式之裝飾者模式_javascript技巧

來源:互聯網
上載者:User

有時我們不希望某個類天生就非常龐大,一次性包含許多職責。那麼我們就可以使用裝飾著模式。
裝飾著模式可以動態地給某個對象添加一些額外的職責,從而不影響這個類中派生的其他對象。
裝飾著模式將一個對象嵌入另一個對象之中,實際上相當於這個對象被另一個對象封裝起來,形成一條封裝鏈。

一、不改動原函數的情況下,給該函數添加些額外的功能

1. 儲存原引用

window.onload = function() {  console.log(1);};var _onload = window.onload || function() {};window.onload = function() {  _onload();  console.log(2);}

問題:
(1)必須維護中間變數
(2)可能遇到this被劫持問題
在window.onload的例子中沒有這個煩惱,是因為調用普通函數_onload時,this也指向window,跟調用window.onload時一樣。

2. this被劫持:

var _getElementById = document.getElementById;document.getElementById = function(id) {  console.log(1);  return _getElementById(id);}return _getElementById(id); // 報錯“Uncaught TypeError: Illegal invocation”

因為_getElementById是全域函數,當調用全域函數時,this是指向window的,而document.getElementById中this預期指向document。

3. 解決this被劫持:

var _getElementById = document.getElementById;document.getElementById = function(id) {  console.log(1);  return _getElementById.call(document, id);}

二、用AOP裝飾函數

/* 讓新添加的函數在原函數之前執行(前置裝飾)*/Function.prototype.before = function(beforefn) {  var _self = this;  return function() {    beforefn.apply(this, arguments);  // 新函數接收的參數會被原封不動的傳入原函數    return _self.apply(this, arguments);  };};
/* 讓新添加的函數在原函數之後執行(後置裝飾)*/Function.prototype.after = function(afterfn) {  var _self = this;  return function() {    var ret = _self.apply(this, arguments);    afterfn.apply(this, arguments);    return ret;  };};
document.getElementById = document.getElementById.before(function() {  console.log(1);});

三、避免汙染原型

var before = function(fn, beforefn) {  return function() {    beforefn.apply(this, arguments);    return fn.apply(this, arguments);  };};var after = function(fn, afterfn) {  return function() {    var ret = fn.apply(this, arguments);    afterfn.apply(this, arguments);    return ret;  };};document.getElementById = before(document.getElementById, function(){  console.log(1);});

四、樣本–外掛程式式的表單驗證

結合《學習JavaScript設計模式之策略模式》中的【表單驗證】,運用到ajax提交資料驗證,效果很棒!

修改上述before方法

var before = function(fn, beforefn) {  return function() {    if(beforefn.apply(this, arguments) === false) {      // beforefn返回false,直接return,不執行後面的原函數      return;    }    return fn.apply(this, arguments);  };};
/* 類比資料驗證*/var validate = function() {  if(username === "") {    console.log("驗證失敗!");    return false;  }  return true;}/* 類比ajax提交*/var formSubmit = function() {  console.log("提交!!!");}
username = 1;formSubmit = before(formSubmit, validate); // 提交!!!formSubmit();username = "";formSubmit = before(formSubmit, validate); // 驗證失敗!formSubmit();

五、裝飾者模式和代理模式

相同點:這兩種模式都描述了怎麼為對象提供一定程度上的間接引用,它們的實現部分都保留了對另外一個對象的引用,並且向那個對象發送請求。
區別:
(1)代理模式:當直接存取本地不方便或者不符合需求時,為這個本體提供一個替代者。本地定義關鍵功能,而代理提供或拒絕對它的訪問,或者在訪問本體之前走一些額外的事情。(其做的事情還是跟本體一樣)
(2)裝飾者模式:為對象動態加入行為。(一開始不能確定對象的全部功能,實實在在的為對象添加新的職責和行為)

希望本文所述對大家學習javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.