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 相关文章推荐
XENON基于JSON变种
Jul 27 Javascript
JSON无限折叠菜单编写实例
Dec 16 Javascript
ExtJS如何设置与获取radio控件的选取状态
Jan 22 Javascript
JS中数组Array的用法示例介绍
Feb 20 Javascript
网站内容禁止复制和粘贴、另存为的js代码
Feb 26 Javascript
jquery的父子兄弟节点查找示例代码
Mar 03 Javascript
纯javascript实现四方向文本无缝滚动效果
Jun 16 Javascript
jQuery实现图片轮播效果代码(基于jquery.pack.js插件)
Jun 02 Javascript
JS利用正则表达式实现简单的密码强弱判断实例
Jun 16 Javascript
基于Vue生产环境部署详解
Sep 15 Javascript
vue.js 添加 fastclick的支持方法
Aug 28 Javascript
vue-router实现编程式导航的代码实例
Jan 19 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
介绍几个array库的新函数 php
2006/12/29 PHP
WordPress后台中实现图片上传功能的实例讲解
2016/01/11 PHP
thinkPHP多语言切换设置方法详解
2016/11/11 PHP
php中的单引号、双引号和转义字符详解
2017/02/16 PHP
PHP利用Cookie设置用户30分钟未操作自动退出功能
2017/07/03 PHP
javascript控制Div层透明属性由浅变深由深变浅逐渐显示
2013/11/12 Javascript
JavaScript实现数组随机排序的方法
2015/06/26 Javascript
javascript每日必学之多态
2016/02/23 Javascript
浅谈JavaScript的push(),pop(),concat()方法
2016/06/03 Javascript
AngularJS定时器的使用与移除操作方法【interval与timeout】
2016/12/14 Javascript
图片懒加载插件实例分享(含解析)
2017/01/09 Javascript
微信小程序 form组件详解及简单实例
2017/01/10 Javascript
bootstrap——bootstrapTable实现隐藏列的示例
2017/01/14 Javascript
ajax分页效果(bootstrap模态框)
2017/01/23 Javascript
JavaScript中的子窗口与父窗口的互相调用问题
2017/02/08 Javascript
浅谈js-FCC算法Friendly Date Ranges(详解)
2017/04/10 Javascript
AngularJS中下拉框的高级用法示例
2017/10/11 Javascript
Javascript组合继承方法代码实例解析
2020/04/02 Javascript
详解vue中v-model和v-bind绑定数据的异同
2020/08/10 Javascript
使用Python下载歌词并嵌入歌曲文件中的实现代码
2015/11/13 Python
Python使用plotly绘制数据图表的方法
2017/07/18 Python
Python中pow()和math.pow()函数用法示例
2018/02/11 Python
python数字类型math库原理解析
2020/03/02 Python
canvas绘制圆角头像的实现方法
2019/01/17 HTML / CSS
Under Armour西班牙官网:美国知名的高端功能性运动品牌
2018/12/12 全球购物
韩国演唱会订票网站:StubHub韩国
2019/01/17 全球购物
荷兰家电购物网站:Expert.nl
2020/01/18 全球购物
实习自我鉴定
2013/12/15 职场文书
劳动实践课感言
2014/02/01 职场文书
2014年反洗钱工作总结
2014/11/22 职场文书
销售员自我评价
2015/03/11 职场文书
升职自我推荐信范文
2015/03/25 职场文书
专家推荐信怎么写
2015/03/25 职场文书
个人自荐书怎么写
2015/03/26 职场文书
2016年五一国际劳动节活动总结
2016/04/06 职场文书
CPU不支持Windows11系统怎么办
2021/11/21 数码科技