學習JavaScript設計模式之裝飾者模式
有時我們不希望某個類天生就非常龐大,一次性包含許多職責。那么我們就可以使用裝飾著模式。
裝飾著模式可以動態(tài)地給某個對象添加一些額外的職責,從而不影響這個類中派生的其他對象。
裝飾著模式將一個對象嵌入另一個對象之中,實際上相當于這個對象被另一個對象包裝起來,形成一條包裝鏈。
一、不改動原函數(shù)的情況下,給該函數(shù)添加些額外的功能
1. 保存原引用
window.onload = function() { console.log(1); }; var _onload = window.onload || function() {}; window.onload = function() { _onload(); console.log(2); }
問題:
(1)必須維護中間變量
(2)可能遇到this被劫持問題
在window.onload的例子中沒有這個煩惱,是因為調用普通函數(shù)_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是全局函數(shù),當調用全局函數(shù)時,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裝飾函數(shù)
/* 讓新添加的函數(shù)在原函數(shù)之前執(zhí)行(前置裝飾)*/ Function.prototype.before = function(beforefn) { var _self = this; return function() { beforefn.apply(this, arguments); // 新函數(shù)接收的參數(shù)會被原封不動的傳入原函數(shù) return _self.apply(this, arguments); }; };
/* 讓新添加的函數(shù)在原函數(shù)之后執(zhí)行(后置裝飾)*/ 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提交數(shù)據(jù)驗證,效果很棒!
修改上述before方法
var before = function(fn, beforefn) { return function() { if(beforefn.apply(this, arguments) === false) { // beforefn返回false,直接return,不執(zhí)行后面的原函數(shù) return; } return fn.apply(this, arguments); }; };
/* 模擬數(shù)據(jù)驗證*/ 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();
五、裝飾者模式和代理模式
相同點:這兩種模式都描述了怎么為對象提供一定程度上的間接引用,它們的實現(xiàn)部分都保留了對另外一個對象的引用,并且向那個對象發(fā)送請求。
區(qū)別:
(1)代理模式:當直接訪問本地不方便或者不符合需求時,為這個本體提供一個替代者。本地定義關鍵功能,而代理提供或拒絕對它的訪問,或者在訪問本體之前走一些額外的事情。(其做的事情還是跟本體一樣)
(2)裝飾者模式:為對象動態(tài)加入行為。(一開始不能確定對象的全部功能,實實在在的為對象添加新的職責和行為)
希望本文所述對大家學習javascript程序設計有所幫助。
相關文章
JavaScript筆記之數(shù)據(jù)屬性和存儲器屬性
本文給大家介紹js數(shù)據(jù)屬性和存儲器屬性,及兩種屬性的區(qū)別,對js數(shù)據(jù)屬性存儲器屬性相關知識感興趣的朋友一起學習2016-03-03JavaScript 異步調用框架 (Part 3 - 代碼實現(xiàn))
在上一篇文章里,我們說到了要實現(xiàn)一個Async.Operation類,通過addCallback方法傳遞回調函數(shù),并且通過yield方法返回回調結果?,F(xiàn)在我們就來實現(xiàn)這個類吧。2009-08-08js模仿windows桌面圖標排列算法具體實現(xiàn)(附圖)
需要引入Jquery,如果需要全部功能,請引入jquery-ui和jquery-ui.css,具體實現(xiàn)步驟如下,感興趣的朋友可以參考下哈2013-06-06