JavaScript接口实现代码 (Interfaces In JavaScript)


Posted in Javascript onJune 11, 2010

在实际中,我们可以在注释中定义好接口,在实际的代码中予以实现
比如:

/* 
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控制swfObject应用介绍
Nov 29 Javascript
jquery mobile changepage的三种传参方法介绍
Sep 13 Javascript
ExtJS的拖拽效果示例
Dec 09 Javascript
js实现跨域的几种方法汇总(图片ping、JSONP和CORS)
Oct 25 Javascript
javascript下拉列表菜单的实现方法
Nov 18 Javascript
JS函数的定义与调用方法推荐
May 12 Javascript
layui选项卡效果实现代码
May 19 Javascript
AngularJS中filter的使用实例详解
Aug 25 Javascript
jQuery实现用户信息表格的添加和删除功能
Sep 12 jQuery
vue-cli3跨域配置的简单方法
Sep 06 Javascript
Vue 通过公共字段,拼接两个对象数组的实例
Nov 07 Javascript
Vue脚手架编写试卷页面功能
Mar 17 Javascript
JavaScript的单例模式 (singleton in Javascript)
Jun 11 #Javascript
超级24小时弹窗代码 24小时退出弹窗代码 100%弹窗代码(IE only)
Jun 11 #Javascript
基于jQuery的js分页代码
Jun 10 #Javascript
基于jQuery的弹出警告对话框美化插件(警告,确认和提示)
Jun 10 #Javascript
jQuery EasyUI中对表格进行编辑的实现代码
Jun 10 #Javascript
jQuery创建自己的插件(自定义插件)的方法
Jun 10 #Javascript
jQuery-ui中自动完成实现方法
Jun 10 #Javascript
You might like
「OVERLORD」动画重要删减!雅儿贝德的背叛?至尊猎杀队结成
2020/04/09 日漫
深入PHP购物车模块功能分析(函数讲解,附源码)
2013/06/25 PHP
php中file_get_contents与curl性能比较分析
2014/11/08 PHP
thinkphp 抓取网站的内容并且保存到本地的实例详解
2017/08/25 PHP
ThinkPHP中图片按比例切割的代码实例
2019/03/08 PHP
PHP实现的微信APP支付功能示例【基于TP5框架】
2019/09/16 PHP
浅谈Laravel POST,PUT,PATCH 路由的区别
2019/10/15 PHP
Javascript实例教程(19) 使用HoTMetal(4)
2006/12/23 Javascript
Javascript 继承实现例子
2009/08/12 Javascript
Javascript的闭包
2009/12/31 Javascript
JavaScript排序算法之希尔排序的2个实例
2014/04/04 Javascript
JS实现1000以内被3或5整除的数字之和
2016/02/18 Javascript
JavaScript获取图片像素颜色并转换为box-shadow显示
2016/03/11 Javascript
关于input全选反选恶心的异常情况
2016/07/24 Javascript
jquery仿微信聊天界面
2017/05/06 jQuery
JavaScript设计模式之单例模式详解
2017/06/09 Javascript
[40:05]DOTA2上海特级锦标赛A组小组赛#1 EHOME VS MVP.Phx第一局
2016/02/25 DOTA
使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子
2014/04/15 Python
Python中实现结构相似的函数调用方法
2015/03/10 Python
浅谈python对象数据的读写权限
2016/09/12 Python
python中的print()输出
2019/04/12 Python
Python3中_(下划线)和__(双下划线)的用途和区别
2019/04/26 Python
在pandas中遍历DataFrame行的实现方法
2019/10/23 Python
python求一个字符串的所有排列的实现方法
2020/02/04 Python
python3.8动态人脸识别的实现示例
2020/09/21 Python
阿迪达斯奥地利官方商城:adidas.at
2016/10/16 全球购物
Alba Moda德国网上商店:意大利时尚女装销售
2016/11/14 全球购物
I.T集团香港官方商城:ITeSHOP.com Hong Kong
2019/02/15 全球购物
美国领先的眼镜和太阳镜在线零售商:Glasses.com
2019/08/26 全球购物
一份全面的PHP面试问题考卷
2012/07/15 面试题
应聘面试自我评价
2014/01/24 职场文书
中青班党性分析材料
2014/02/16 职场文书
婚纱摄影师求职信范文
2014/04/17 职场文书
纪念九一八事变演讲稿:牢记历史,捍卫主权
2014/09/14 职场文书
于丹讲座视频观后感
2015/06/15 职场文书
一些让Python代码简洁的实用技巧总结
2021/08/23 Python