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

JavaScript接口實(shí)現(xiàn)代碼 (Interfaces In JavaScript)

在實(shí)際中,我們可以在注釋中定義好接口,在實(shí)際的代碼中予以實(shí)現(xiàn)
比如:
復(fù)制代碼 代碼如下: /*
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() {
...
};

實(shí)現(xiàn)接口的程序員是否將這些接口都實(shí)現(xiàn)了呢?我們沒辦法保證!因?yàn)檫@里沒有任何辦法去檢查是否都實(shí)現(xiàn)了
我們需要一個(gè)檢查是否實(shí)現(xiàn)了接口的機(jī)制,可以這樣:
復(fù)制代碼 代碼如下: /*
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.
}
這種方法讓程序員在寫的時(shí)候注明實(shí)現(xiàn)了哪些接口: this.implementsInterfaces = ['Composite', 'FormItem']; 在調(diào)用的時(shí)候使用implements方法來判斷是否實(shí)現(xiàn)了,理論上可行,很有可能寫上了實(shí)現(xiàn)了'Composite'接口,但是代碼里卻并沒有add方法!因此,我們需要檢驗(yàn)實(shí)現(xiàn)接口的類是否包含了接口里的方法!所以,接口必須從注釋中解放出來:
復(fù)制代碼 代碼如下: // 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,F(xiàn)ormItem,并且CompositeForm實(shí)現(xiàn)這兩個(gè)接口,在使用的時(shí)候,用Interface.ensureImplements來檢驗(yàn)formInstance是否實(shí)現(xiàn)了這兩個(gè)接口中的所有方法。
來看看Interface的定義:兩個(gè)參數(shù),第一個(gè)參數(shù)是接口名稱,第二個(gè)參數(shù)是接口包含的方法數(shù)組
復(fù)制代碼 代碼如下: // 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 添加建議接口是否實(shí)現(xiàn)的靜態(tài)方法
復(fù)制代碼 代碼如下: // 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技術(shù)JavaScript接口實(shí)現(xiàn)代碼 (Interfaces In JavaScript),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 欧美成人免费一区二区三区不卡 | 99热国产这里只有精品9九 | 不良网站进入窗口软件下载免费 | 色www永久免费 | 青青久| 亚洲嫩草影院久久精品 | 久久AV国产麻豆HD真实乱 | 99精品免费久久久久久久久日本 | 国产又色又爽又刺激在线播放 | 五月丁香啪啪. | 午夜福利小视频400 午夜福利网国产A | 久久人妻熟女中文字幕AV蜜芽 | 国产69精品久久久久人妻刘玥 | 美女挑战50厘米长的黑人 | 国产成人高清在线观看播放 | 欧美伊人久久大香线蕉综合69 | a视频在线免费观看 | 小p孩玩成年女性啪啪资源 小777论坛 | 亚洲国产精品无码中文字满 | 色青青草原桃花久久综合 | 在线播放午夜理论片 | 久久亚洲AV无码精品午色夜麻豆 | 精品亚洲国产成AV人片传媒 | 色多多污污版免费下载安装 | 国产精品人妻无码99999 | 丰满的女朋友 在线播放 | 公和熄洗澡三级中文字幕 | 4399的视频BD高清在线观看免费 | 国语自产拍在线视频普通话 | 2019夜夜| 色www精品视频在线观看 | 男人的天堂久久精品激情a 男人的天堂黄色片 | 亚洲精品色情婷婷在线播放 | 亚洲综合AV色婷婷五月蜜臀 | 国产av在在免费线观看美女 | 日本吃孕妇奶水免费观看 | 亚洲中文字幕在线第六区 | 亚洲国产成人精品无码区99 | 男gv纯肉免费视频 | 99热精品在线视频观看 | 飘雪韩国在线观看免费高清完整版 |