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 Event学习第十一章 按键的检测
Feb 10 Javascript
Javascript中获取出错代码所在文件及行数的代码
Sep 23 Javascript
js判断字符是否是汉字的两种方法小结
Jan 03 Javascript
JQuery 使用attr方法实现下拉列表选中
Oct 13 Javascript
JavaScript事件学习小结(一)事件流
Jun 09 Javascript
Angular ng-class详解及实例代码
Sep 19 Javascript
vue2.0 根据状态值进行样式的改变展示方法
Mar 13 Javascript
基于Vue实现微信小程序的图文编辑器
Jul 25 Javascript
深入学习JavaScript中的bom
May 27 Javascript
JavaScript实现英语单词题库
Dec 24 Javascript
详解Vue数据驱动原理
Nov 17 Javascript
了不起的11个JavaScript代码重构最佳实践小结
Jan 11 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
DOM基础及php读取xml内容操作的方法
2015/01/23 PHP
一个JQuery操作Table的代码分享
2012/03/30 Javascript
js中eval()函数和trim()去掉字符串左右空格应用
2013/02/02 Javascript
js前台分页显示后端JAVA数据响应
2013/03/18 Javascript
JavaScript输入邮箱自动提示实例代码
2014/01/13 Javascript
JavaScript中双叹号(!!)作用示例介绍
2014/04/10 Javascript
JQEasy-ui在IE9以下版本中二次加载的问题分析及处理方法
2014/06/23 Javascript
JavaScript定时显示广告代码分享
2015/03/02 Javascript
jquery验证邮箱格式并显示提交按钮
2015/11/07 Javascript
json格式的javascript对象用法分析
2016/07/04 Javascript
jQuery Dialog 取消右上角删除按钮事件
2016/09/07 Javascript
谈谈vue中mixin的一点理解
2017/12/12 Javascript
基于vue框架手写一个notify插件实现通知功能的方法
2019/03/31 Javascript
微信小程序开发搜索功能实现(前端+后端+数据库)
2020/03/04 Javascript
jQuery中getJSON跨域原理的深入讲解
2020/09/02 jQuery
[01:00:11]DOTA2-DPC中国联赛 正赛 CDEC vs DLG BO3 第一场 2月7日
2021/03/11 DOTA
探究Python中isalnum()方法的使用
2015/05/18 Python
Python删除windows垃圾文件的方法
2015/07/14 Python
python学生管理系统代码实现
2020/04/05 Python
Python中Numpy包的安装与使用方法简明教程
2018/07/03 Python
Python 从attribute到property详解
2020/03/05 Python
Python稀疏矩阵及参数保存代码实现
2020/04/18 Python
Python命名空间namespace及作用域原理解析
2020/06/05 Python
python开发入门——set的使用
2020/09/03 Python
css3动画过渡实现鼠标跟随导航效果
2018/02/08 HTML / CSS
英国奢华护肤、美容和Spa品牌:Temple Spa
2019/11/02 全球购物
黄继光的英雄事迹材料
2014/02/13 职场文书
党的群众路线教育实践活动宣传方案
2014/02/23 职场文书
贸易跟单员英文求职信
2014/04/19 职场文书
巴西世界杯32强口号
2014/06/05 职场文书
三月学雷锋活动总结
2014/06/26 职场文书
公司行政专员岗位职责
2014/08/24 职场文书
中学生的1000字检讨书
2014/10/11 职场文书
幼师中班个人总结
2015/02/12 职场文书
毕业论文答辩稿范文
2015/06/23 职场文书
学术会议开幕词
2016/03/03 职场文书