您现在的位置是:Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款 >
01 js發布訂閱模式代碼(js監聽事件有什么用js監聽事件有什么用處)
Instagram刷粉絲, Ins買粉絲自助下單平台, Ins買贊網站可微信支付寶付款2024-07-27 09:26:32【】8人已围观
简介Javascript如何實現接口?在javascript中并沒有原生的創建或者實現接口的方式,或者判定一個類型是否實現了某個接口,我們只能利用js的靈活性的特點,模擬接口。在javascript中實現
Javascript如何實現接口?
在javascript中并沒有原生的創建或者實現接口的方式,或者判定一個類型是否實現了某個接口,我們只能利用js的靈活性的特點,模擬接口。
在javascript中實現接口有三種方式:注釋描述、屬性驗證、鴨子模型。
note:因為我看的是英文書,翻譯水平有限,不知道有些詞匯如何翻譯,大家只能領會精神了。
1. 注釋描述 (Describing Interfaces with Comments)
例子:
復制代碼 代碼如下:
/*
interface Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};
//Implement the Composite interface.
CompositeForm.prototype.add = function(child) {
...
};
CompositeForm.prototype.remove = function(child) {
...
};
CompositeForm.prototype.getChild = function(index) {
...
};
// Implement the FormItem interface.
CompositeForm.prototype.save = function() {
...
};
模擬其他面向對象語言,使用interface 和 implements關鍵字,但是需要將他們注釋起來,這樣就不會有語法錯誤。
這樣做的目的,只是為了告訴其他編程人員,這些類需要實現什么方法,需要在編程的時候加以注意。但是沒有提供一種驗證方式,這些類是否正確實現了這些接口中的方法,這種方式就是一種文檔化的作法。
2. 屬性驗證(Emulating Interfaces with Attribute Checking)
例子:
復制代碼 代碼如下:
/* interface
Composite {
function add(child);
function remove(child);
function getChild(index);
}
interface FormItem {
function save();
}
*/
var CompositeForm = function(id, method, action) {
this.implementsInterfaces = ['Composite', 'FormItem'];
...
};
...
function addForm(formInstance) {
if(!implements(formInstance, 'Composite', 'FormItem')) {
throw new Error("Object does not implement a required interface.");
}
...
}
// 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 " + metho
很赞哦!(31)
相关文章
- 01 蘇州浩維銳貿易有限公司的銷售怎么樣(蘇州浩維銳貿易公司可靠嗎)
- 03 youtube下載轉mp3免費網頁(PasteDownload 免費網頁影片下載器,支援上百個影音社交網站)
- 03 youtube下載網站排行榜前200年(韓國KBS,SBS,MBC三大電視臺的 Music Bank,人氣歌謠,音樂中心的打歌的規則。)
- 03 youtube下載網站排行榜前100男(歷史上最厲害電腦病毒)
- 01 蘇州博士特貿易有限公司(在中國的著名德國企業有哪些)
- 01 蘇州悅性貿易有限公司(蘇州尼依格羅酒店開業時間+地址)
- 03 youtube官網 中文新聞數據庫表設計(高分懸賞:著名演講哪里去下?)
- 03 youtube買粉絲下載軟件被識別(蘋果手機升級后里面的東西是不是沒有了)
- 01 蘇州德比貿易有限公司(蘇州德比光伏新材料科技有限公司電話是多少?)
- 03 youtube下載轉mp3在線轉換(怎樣從網站上下載在線播放的視頻)
热门文章
站长推荐
03 youtube下載網站安全服務平臺下載百(background intelligent transfer service 關閉這項服務有什么弊端?)
01 花西子海外營銷環境(花西子的營銷是如何針對消費者的偏好來進行設計的?)
03 youtube中文版香港歷史教材(周瑞發!)
01 花橋地鐵到上海外灘坐幾號線(從昆山花橋坐地鐵到上海外灘怎么坐 哪站下)
01 蘇州交警買粉絲買粉絲預約怎么取消(紅綠燈設計不合理?“蘇州交警”買粉絲買粉絲上能反饋啦!)
01 蘇州第五人民醫院買粉絲(蘇州駕駛證換證體檢醫院有哪些)
03 youtube下載網站排行榜第12(網絡推廣主要渠道有哪些,我說的是免費的。除了在自己網站發軟文還有別的嗎)
03 youtube下載網站安全證書是誰頒發(網絡推廣主要渠道有哪些,我說的是免費的。除了在自己網站發軟文還有別的嗎)