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 相关文章推荐
json简单介绍
Jun 10 Javascript
火狐浏览器(firefox)下获得Event对象以及keyCode
Nov 13 Javascript
JavaScript 闭包深入理解(closure)
May 27 Javascript
JQuery 拾色器插件发布-jquery.icolor.js
Oct 20 Javascript
jquery随意添加移除html的实现代码
Jun 21 Javascript
javascript的解析执行顺序在各个浏览器中的不同
Mar 17 Javascript
JavaScript中停止执行setInterval和setTimeout事件的方法
May 14 Javascript
详解照片瀑布流效果(js,jquery分别实现与知识点总结)
Jan 01 Javascript
swiper在angularjs中使用循环轮播失效的解决方法
Sep 27 Javascript
layui监听工具栏的实例(操作列表按钮)
Sep 10 Javascript
js实现无缝轮播图效果
Mar 09 Javascript
js 实现验证码输入框示例详解
Sep 23 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模糊查询技术实例分析【附源码下载】
2019/03/07 PHP
input 输入框内的输入事件详细分析
2010/03/17 Javascript
js判断undefined类型,undefined,null, 的区别详细解析
2013/12/16 Javascript
javascript打开word文档的方法
2014/04/16 Javascript
NodeJS学习笔记之Connect中间件应用实例
2015/01/27 NodeJs
Javascript调用函数方法的几种方式介绍
2015/03/20 Javascript
原生javascript实现解析XML文档与字符串
2016/03/01 Javascript
浅谈js内置对象Math的属性和方法(推荐)
2016/09/19 Javascript
Angular ng-class详解及实例代码
2016/09/19 Javascript
JS根据生日月份和日期计算星座的简单实现方法
2016/11/24 Javascript
Bootstrap CSS布局之按钮
2016/12/17 Javascript
jquery.validate.js 多个相同name的处理方式
2017/07/10 jQuery
Vue中render函数的使用方法
2018/01/31 Javascript
JavaScript学习笔记之图片库案例分析
2019/01/08 Javascript
JavaScript格式化json和xml的方法示例
2019/01/22 Javascript
浅入深出Vue之组件使用
2019/07/11 Javascript
浅谈TypeScript 用 Webpack/ts-node 运行的配置记录
2019/10/11 Javascript
详解Python迭代和迭代器
2016/03/28 Python
用tensorflow搭建CNN的方法
2018/03/05 Python
Python使用Django实现博客系统完整版
2020/09/29 Python
Python设计模式之桥接模式原理与用法实例分析
2019/01/10 Python
Python实现最大子序和的方法示例
2019/07/05 Python
python爬虫 2019中国好声音评论爬取过程解析
2019/08/26 Python
Python 实现训练集、测试集随机划分
2020/01/08 Python
使用python求解二次规划的问题
2020/02/29 Python
python pygame 愤怒的小鸟游戏示例代码
2021/02/25 Python
关于HTML5 Placeholder新标签低版本浏览器下不兼容的问题分析及解决办法
2016/01/27 HTML / CSS
夏洛特和乔治婴儿和儿童时装精品店:Charlotte and George
2018/06/06 全球购物
汽车队司机先进事迹材料
2014/02/01 职场文书
化妆品促销方案
2014/02/24 职场文书
成龙霸王洗发水广告词
2014/03/14 职场文书
对孩子的寄语
2014/04/09 职场文书
厂区绿化方案
2014/05/08 职场文书
MySQL安装后默认自带数据库的作用详解
2021/04/27 MySQL
Python实现单例模式的5种方法
2021/06/15 Python
Win11 PC上的Outlook搜索错误怎么办?
2022/07/15 数码科技