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 相关文章推荐
jQuery的三种$()
Dec 30 Javascript
JavaScript Chart 插件整理
Jun 18 Javascript
js批量设置样式的三种方法不推荐使用with
Feb 25 Javascript
JS小功能(button选择颜色)简单实例
Nov 29 Javascript
JQuery做的一个简单的点灯游戏分享
Jul 16 Javascript
JS+CSS实现Div弹出窗口同时背景变暗的方法
Mar 04 Javascript
js轮盘抽奖实例分析
Apr 17 Javascript
微信小程序 SocketIO 实例讲解
Oct 13 Javascript
jQuery each和js forEach用法比较
Feb 27 jQuery
Node在Controller层进行数据校验的过程详解
Aug 28 Javascript
js实现简易拖拽的示例
Oct 26 Javascript
JS中一些高效的魔法运算符总结
May 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
PR值查询 | PageRank 查询
2006/12/20 PHP
用PHP实现的生成静态HTML速度快类库
2007/03/31 PHP
PHP伪静态页面函数附使用方法
2008/06/20 PHP
php中多维数组按指定value排序的实现代码
2014/08/19 PHP
PHP IDE PHPStorm配置支持友好Laravel代码提示方法
2015/05/12 PHP
WordPress中给媒体文件添加分类和标签的PHP功能实现
2015/12/31 PHP
php添加数据到xml文件的简单例子
2016/09/08 PHP
PHP magento后台无法登录问题解决方法
2016/11/24 PHP
thinkphp5 migrate数据库迁移工具
2018/02/20 PHP
php中使用array_filter()函数过滤数组实例讲解
2021/03/03 PHP
基于jquery的当鼠标滚轮到最底端继续加载新数据思路分享(多用于微博、空间、论坛 )
2011/10/10 Javascript
fixedBox固定div漂浮代码支持ie6以上大部分主流浏览器
2014/06/26 Javascript
js实现同一个页面多个渐变效果的方法
2015/04/10 Javascript
ECMAScript5(ES5)中bind方法使用小结
2015/05/07 Javascript
jQuery数组处理函数整理
2016/08/03 Javascript
AngularJS 限定$scope的范围实例详解
2017/06/23 Javascript
JQuery判断正整数整理小结
2017/08/21 jQuery
nodejs 使用 js 模块的方法实例详解
2018/12/04 NodeJs
angular4中引入echarts的方法示例
2019/01/29 Javascript
[43:49]LGD vs CHAOS 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
linux系统使用python监测网络接口获取网络的输入输出
2014/01/15 Python
Python编程中字符串和列表的基本知识讲解
2015/10/14 Python
玩转python爬虫之cookie使用方法
2016/02/17 Python
python reduce 函数使用详解
2017/12/05 Python
在scrapy中使用phantomJS实现异步爬取的方法
2018/12/17 Python
python 自动批量打开网页的示例
2019/02/21 Python
Python面向对象之继承和多态用法分析
2019/06/08 Python
解决django中ModelForm多表单组合的问题
2019/07/18 Python
python 如何将office文件转换为PDF
2020/09/22 Python
VC++笔试题
2014/10/13 面试题
C/C++ 笔试、面试题目大汇总
2015/11/21 面试题
村官学习十八大感想
2014/01/15 职场文书
医学生毕业自我鉴定
2014/03/26 职场文书
任命书模板
2014/06/04 职场文书
保密工作目标责任书
2014/07/28 职场文书
北京爱情故事观后感
2015/06/12 职场文书