您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
02 js訂閱模式和觀察者模式(js 怎么理解js事件觀察者模式)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-06-22 12:54:58【】0人已围观
简介ectdoesnotimplementarequiredinterface.");}...}//Theimplementsfunction,whichcheckstoseeifanobjec
}
...
}
// The implements function, which checks to see if an object declares that it
// implements the required interfaces.
function implements(object) {
for(var i = 1; i < arguments.length; i++) {
// Looping through all arguments
// after the first one.
var interfaceName = arguments[i];
var interfaceFound = false;
for(var j = 0; j < object.implementsInterfaces.length; j++) {
if(object.implementsInterfaces[j] == interfaceName) {
interfaceFound = true;
break;
}
}
if(!interfaceFound) {
return false;
// An interface was not found.
}
}
return true;
// All interfaces were found.
}
這種方式比第一種方式有所改進,接口的定義仍然以注釋的方式實現,但是添加了驗證方法,判斷一個類型是否實現了某個接口。
3.鴨子類型(Emulating Interfaces with Duck Typing)
復制代碼 代碼如下:
// Interfaces.
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);
// CompositeForm class
var CompositeForm = function(id, method, action) {
...
};
...
function addForm(formInstance) {
ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented.
...
}
// Constructor.
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface 買粉絲nstructor called with "
+ arguments.length + "arguments, but expected exactly 2.");
}
this.name = name;
this.methods = [];
for(var i = 0, len = methods.length; i < len; i++) {
if(typeof methods[i] !== 'string') {
throw new Error("Interface 買粉絲nstructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};
// Static class method.
Interface.ensureImplements = function(object) {
if(arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with "
+arguments.length + "arguments, but expected at least 2.");
}
for(var i = 1, len = arguments.length; i < len; i++) {
var interface = arguments[i];
if(interface.買粉絲nstructor !== Interface) {
throw new Error("Function Interface.ensureImplements expects arguments"
+ "two and above to be instances of Interface.");
}
for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++) {
var method = interface.methods[j];
if(!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name + " interface. Method " + method + " was not found.");
}
}
}
};
何時使用接口?
一直使用嚴格的類型驗證并不適合,因為大多數javascript程序員已經在沒有接口和接口驗證的情況下編程多年。當你用設計模式開始設計一個很復雜的系統的時候,使用接口更有益處。看起來使用接口好像限制了javascript的靈活性,但實際上他讓你的代碼變得更加的松耦合。他使你的代碼變得更加靈活,你可以傳送任何類型的變量,并且保證他有你想要的方法。有很多場景接口非常適合使用。
在一個大型系統里,很多程序員一起參與開發項目,接口就變得非常必要了。程序員經常要訪問一個還沒有實現的api,或者為其他程序員提供別人依賴的一個方法存根,在這種情況下,接口變得相當的有價值。他們可以文檔化api,并作為編程的契約。當存根被實現的api替換的時候你能立即知道,如果在開發過程中api有所變動,他能被另一個實現該接口的方法無縫替換。
如何使用接口?
首先要解決的問題是,在你的代碼中是否適合使用接口。如果是小項目,使用接口會增加代碼的復雜度。所以你要確定使用接口的情況下,是否是益處大于弊端。如果要使用接口,下面有幾條建議:
1.引用Interface 類到你的頁面文件。interface的源文件你可以再如下站點找到: 買粉絲://jsdesignpatterns.買粉絲/.
2.檢查你的代碼,確定哪些方法需要抽象到接口里面。
3.創建接口對象,沒個接口對象里面包含一組相關的方法。
4.移除所有構造器驗證,我們將使用第三種接口實現方式,也就是鴨子類型。
5.用Interface.ensureImplements替代構造器驗證。
您可能感興趣的文章:
小議javascript 設計模式 推薦
JavaScript 設計模式之組合模式解析
javascript 設計模式之單體模式 面向對象學習基礎
JavaScript 設計模式 安全沙箱模式
JavaScript設計模式之觀察者模式(發布者-訂閱者模式)
JavaScript設計模式之原型模式(Object.create與prototype)介紹
JavaScript設計模式之工廠方法模式介紹
javascript設計模式之中介者模式Mediator
學習JavaScript設計模式之責任鏈模式
很赞哦!(37145)
相关文章
- 01 youtube視頻下載器高清線怎么連接(怎樣從網站上下載在線播放的視頻)
- windows 10 youtube 下載qq(或下載器現在升級是什么軟件)
- www.youtub.com.tw(臺灣最大的視頻網站)
- 01 youtube視頻下載器高清線hdmi區別(游戲達人進 x360達人進)
- what does tiktok show in china是幾(推薦幾首好聽的英文歌曲~~~~)
- why youtube isn't working(英語流利說Level7 unit1 part1 On procrastination)
- what is youtube considered(《經濟學人》精讀19:Me, myself and iPhone)
- 01 youtube視頻下載器高清線轉多段(如何將h265轉換為h264格式的視頻?)
- 01 youtube視頻下載器高清接口轉換vga(ipad有什么使用功能?)
- windows 10 破解 youtube下載官網(或下載器現在升級是什么軟件)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款的名片
职业:程序员,设计师
现居:河北省邯郸邯山区
工作室:小组
Email:[email protected]
热门文章
站长推荐
01 youtube視頻下載器軟件開發工程(一般什么視頻剪輯軟件好用,有誰知道Youtube達人一般用什么剪輯軟件制作的視頻。)
win10在youtube上下載視頻的方法(win10用MicrosoftEdge瀏覽器瀏覽YouTube速度很慢怎么辦)
01 youtube視頻下載器教程詳解pdf網盤(下載油管視頻提示找不到文件)
01 youtube視頻下載器教程視頻教程入門到精通(有沒有抖音剪輯培訓教程,還有抖音書單培訓教程,書單文案如何原創?)
www tiktok com下載(iphone怎么下載tiktok)
01 youtube視頻下載在線軟件(怎么在youtube上下載視頻)
what is youtube hk(初識數學大師柯爾莫哥洛夫(Kolmogorov))
01 youtube視頻下載在線(請教如何下載加密視頻)