天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

JavaScript接口實現代碼 (Interfaces In JavaScript)

在實際中,我們可以在注釋中定義好接口,在實際的代碼中予以實現
比如:
復制代碼 代碼如下: /*
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 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.
}
這種方法讓程序員在寫的時候注明實現了哪些接口: this.implementsInterfaces = ['Composite', 'FormItem']; 在調用的時候使用implements方法來判斷是否實現了,理論上可行,很有可能寫上了實現了'Composite'接口,但是代碼里卻并沒有add方法!因此,我們需要檢驗實現接口的類是否包含了接口里的方法!所以,接口必須從注釋中解放出來:
復制代碼 代碼如下: // Interfaces.
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']);
var FormItem = new Interface('FormItem', ['save']);
// CompositeForm class
var CompositeForm = function(id, method, action) { // implements Composite, FormItem
...
};
...
function addForm(formInstance) {
Interface.ensureImplements(formInstance, Composite, FormItem);
// This function will throw an error if a required method is not implemented,
// halting execution of the function.
// All code beneath this line will be executed only if the checks pass.
...
}
定義接口Composite,FormItem,并且CompositeForm實現這兩個接口,在使用的時候,用Interface.ensureImplements來檢驗formInstance是否實現了這兩個接口中的所有方法。
來看看Interface的定義:兩個參數,第一個參數是接口名稱,第二個參數是接口包含的方法數組
復制代碼 代碼如下: // Constructor.
var Interface = function(name, methods) {
if(arguments.length != 2) {
throw new Error("Interface constructor 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 constructor expects method names to be "
+ "passed in as a string.");
}
this.methods.push(methods[i]);
}
};
為Interface 添加建議接口是否實現的靜態方法
復制代碼 代碼如下: // Constructor.
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.constructor !== 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接口實現代碼 (Interfaces In JavaScript),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: japonensis护士| 人妻换人妻AA视频 | 亚洲精品一区国产欧美 | 日韩欧美中文字幕在线 | 天天狠狠色综合图片区 | 超大BBWWW| 交换年轻夫妇HD中文字幕 | 精品久久久亚洲精品中文字幕 | 国产蜜臀AV在线一区视频 | 欧美日韩无套内射另类 | 欧美 亚洲 日韩 在线综合 | 国色天香视频在线社区 | 国产精品久久自在自2021 | 97se se| 91福利潘春春在线观看 | 国产成人久久婷婷精品流白浆 | 在线视频 日韩视频二区 | 视频区 国产 欧美 日韩 | 特级做A爰片毛片免费看108 | 精品一区二区三区高清免费观看 | 免费A级毛片无码无遮挡内射 | 97在线看视频福利免费 | 娇小萝被两个黑人用半米长 | 欧美人与动交zOZ0 | 在线播放性xxx欧美 在线播放午夜理论片 | 网红刘婷hd国产高清 | 亚洲高清毛片一区二区 | 亚洲精品一区国产欧美 | 日本国产成人精品无码区在线网站 | 蜜臀久久99精品久久久久久做爰 | 成年人视频在线免费 | 亚洲精品无码一区二区三区四虎 | 亚洲精品在线免费 | 亚洲精品一二三区区别在哪 | 美女被男人撕衣舔胸 | 欧美四虎精品二区免费 | 在线观看成年人免费视频 | 伦理片天堂eeuss影院2o12 | 久久九九精品国产自在现线拍 | 灌饱娇嫩H将军公主最新章节 | 一本色道久久综合亚洲精品蜜桃冫 |