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 相关文章推荐
JsDom 编程小结
Aug 09 Javascript
利用webqq协议使用python登录qq发消息源码参考
Apr 08 Javascript
JavaScript异步编程:异步数据收集的具体方法
Aug 19 Javascript
图片动画横条广告带上下滚动可自定义图片、链接等等
Oct 20 Javascript
深入理解JavaScript中的call、apply、bind方法的区别
May 30 Javascript
浅析Javascript中bind()方法的使用与实现
May 30 Javascript
Javascript ES6中数据类型Symbol的使用详解
May 02 Javascript
vue事件修饰符和按键修饰符用法总结
Jul 25 Javascript
Vue自定义指令上报Google Analytics事件统计的方法
Feb 25 Javascript
详解ng-alain动态表单SF表单项设置必填和正则校验
Jun 11 Javascript
浅谈Webpack4 Tree Shaking 终极优化指南
Nov 18 Javascript
JS 数组和对象的深拷贝操作示例
Jun 06 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保存带BOM文件的方法
2015/02/12 PHP
PHP使用redis消息队列发布微博的方法示例
2017/06/22 PHP
javascript 常用方法总结
2009/06/03 Javascript
编写高性能Javascript代码的N条建议
2015/10/12 Javascript
老生常谈JavaScript数组的用法
2016/06/10 Javascript
Js删除数组中某一项或几项的几种方法(推荐)
2016/07/27 Javascript
js从外部获取图片的实现方法
2016/08/05 Javascript
AngularJS bootstrap启动详解及实例代码
2016/09/14 Javascript
react性能优化达到最大化的方法 immutable.js使用的必要性
2017/03/09 Javascript
js学习总结之DOM2兼容处理重复问题的解决方法
2017/07/27 Javascript
详解js静态资源文件请求的处理
2017/08/01 Javascript
基于VUE.JS的移动端框架Mint UI的使用
2017/10/11 Javascript
jQuery+datatables插件实现ajax加载数据与增删改查功能示例
2018/04/17 jQuery
深入浅析Node.js 事件循环、定时器和process.nextTick()
2018/10/22 Javascript
简单了解JavaScript异步
2019/05/23 Javascript
Layui table field初始化加载时进行隐藏的方法
2019/09/19 Javascript
vue 解决数组赋值无法渲染在页面的问题
2019/10/28 Javascript
Node.js创建一个Express服务的方法详解
2020/01/06 Javascript
uni-app实现获取验证码倒计时功能
2020/11/01 Javascript
Python的Flask框架中实现简单的登录功能的教程
2015/04/20 Python
python实现电脑自动关机
2018/06/20 Python
Python读取csv文件实例解析
2019/12/30 Python
django 扩展user用户字段inlines方式
2020/03/30 Python
使用OpenCV获取图像某点的颜色值,并设置某点的颜色
2020/06/02 Python
python使用requests库爬取拉勾网招聘信息的实现
2020/11/20 Python
HTML利用九宫格原理进行网页布局
2020/03/13 HTML / CSS
Boden英国官网:英国知名原创时装品牌
2018/11/06 全球购物
Java基础知识面试题
2014/03/25 面试题
大学生职业规划书的范本
2014/02/18 职场文书
投资合作协议书范本
2014/04/17 职场文书
社保代办委托书怎么写
2014/10/06 职场文书
人与自然的观后感
2015/06/18 职场文书
廉洁自律准则学习心得体会
2016/01/13 职场文书
python实现高效的遗传算法
2021/04/07 Python
浅谈golang 中time.After释放的问题
2021/05/05 Golang
Go调用Rust方法及外部函数接口前置
2022/06/14 Golang