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 相关文章推荐
获取Javscript执行函数名称的方法
Dec 22 Javascript
jQuery 动画基础教程
Dec 25 Javascript
仿新浪微博登陆邮箱提示效果的js代码
Aug 02 Javascript
JavaScript数组迭代器实例分析
Jun 09 Javascript
浅谈Javascript中Object与Function对象
Sep 26 Javascript
jquery设置css样式的多种方法(总结)
Feb 21 Javascript
vue项目中的webpack-dev-sever配置方法
Dec 14 Javascript
浅谈PDF.js使用心得
Jun 07 Javascript
JS逻辑运算符短路操作实例分析
Jul 09 Javascript
Vue 莹石摄像头直播视频实例代码
Aug 31 Javascript
微信小程序实现页面跳转传递参数(实体,对象)
Aug 12 Javascript
js实现ajax的用户简单登入功能
Jun 18 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
php判断页面是否是微信打开的示例(微信打开网页)
2014/04/25 PHP
在Win7 中为php扩展配置Xcache
2014/10/08 PHP
ci检测是ajax还是页面post提交数据的方法
2014/11/10 PHP
php可应用于面包屑导航的递归寻找家谱树实现方法
2015/02/02 PHP
CI框架(CodeIgniter)实现的数据库增删改查操作总结
2018/05/23 PHP
php5.6.x到php7.0.x特性小结
2019/08/17 PHP
PHP7数组的底层实现示例
2019/08/25 PHP
Div Select挡住的解决办法
2008/08/07 Javascript
Javascript 拖拽雏形中的一些问题(逐行分析代码,让你轻松了拖拽的原理)
2015/01/23 Javascript
基于jQuery实现弹出可关闭遮罩提示框实例代码
2016/07/18 Javascript
快速实现JS图片懒加载(可视区域加载)示例代码
2017/01/04 Javascript
微信小程序实战之运维小项目
2017/01/17 Javascript
Vue前后端不同端口的实现方法
2018/09/19 Javascript
vue-cli项目配置多环境的详细操作过程
2018/10/30 Javascript
trackingjs+websocket+百度人脸识别API实现人脸签到
2018/11/26 Javascript
nodejs npm错误Error:UNKNOWN:unknown error,mkdir 'D:\Develop\nodejs\node_global'at Error
2019/03/02 NodeJs
python进阶教程之词典、字典、dict
2014/08/29 Python
Python实现的微信公众号群发图片与文本消息功能实例详解
2017/06/30 Python
centos6.4下python3.6.1安装教程
2017/07/21 Python
Python 修改列表中的元素方法
2018/06/26 Python
python绘制直线的方法
2018/06/30 Python
Python3编码问题 Unicode utf-8 bytes互转方法
2018/10/26 Python
Pandas DataFrame 取一行数据会得到Series的方法
2018/11/10 Python
Python中使用filter过滤列表的一个小技巧分享
2020/05/02 Python
举例讲解Python装饰器
2020/12/24 Python
基于Pytorch版yolov5的滑块验证码破解思路详解
2021/02/25 Python
沪江旗下的海量优质课程平台:沪江网校
2017/11/07 全球购物
解除合同协议书
2014/04/17 职场文书
中国梦我的梦演讲稿
2014/04/23 职场文书
设计专业毕业生求职信
2014/06/25 职场文书
英语课外活动总结
2014/08/27 职场文书
市委常委会班子党的群众路线教育实践活动整改方案
2014/10/25 职场文书
写作之关于描写老人的好段摘抄
2019/11/14 职场文书
Mysql 如何批量插入数据
2021/04/06 MySQL
SONY AN-LP1 短波有源天线放大器
2021/04/22 无线电
Nginx 502 bad gateway错误解决的九种方案及原因
2022/08/14 Servers