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实例教程(19) 使用HoTMetal(6)
Dec 23 Javascript
Prototype中dom对象方法汇总
Sep 17 Javascript
jquery中常用的SET和GET
Jan 13 Javascript
js创建对象的几种常用方式小结(推荐)
Oct 24 Javascript
Jquery工作常用实例 使用AJAX使网页进行异步更新
Jul 26 Javascript
如何让页面在打开时自动刷新一次让图片全部显示
Dec 17 Javascript
JavaScript实现梯形乘法表的方法
Apr 25 Javascript
在Python中使用glob模块查找文件路径的方法
Jun 17 Javascript
Bootstrap幻灯片轮播图支持触屏左右手势滑动的实现方法
Oct 13 Javascript
React Router v4 入坑指南(小结)
Apr 08 Javascript
详解在Javascript中进行面向切面编程
Apr 28 Javascript
Angular 2使用路由自定义弹出组件toast操作示例
May 10 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基础教程 php内置函数实例教程
2012/08/21 PHP
codeigniter集成ucenter1.6双向通信的解决办法
2014/06/12 PHP
Ubuntu12下编译安装PHP5.3开发环境
2015/03/27 PHP
php实现的mongoDB单例模式操作类
2018/01/20 PHP
浅析PHP echo 和 print 语句
2020/06/30 PHP
经常用到的JavasScript事件的翻译
2007/04/09 Javascript
JQuery中的$.getJSON 使用说明
2011/03/10 Javascript
jQuery中需要注意的细节问题小结
2011/12/06 Javascript
JavaScript charCodeAt方法入门实例(用于取得指定位置字符的Unicode编码)
2014/10/17 Javascript
NodeJS学习笔记之Connect中间件模块(一)
2015/01/27 NodeJs
JavaScript将字符串转换成字符编码列表的方法
2015/03/19 Javascript
jQuery插件bxSlider实现响应式焦点图
2015/04/12 Javascript
angularjs ui-router中路由的二级嵌套
2017/03/10 Javascript
JS将unicode码转中文方法
2017/05/08 Javascript
详解Vue-Cli 异步加载数据的一些注意点
2017/08/12 Javascript
使用 Vue-TCB 快速在 Vue 应用中接入云开发的方法
2020/02/10 Javascript
云服务器部署Node.js项目的方法步骤(小白系列)
2020/03/23 Javascript
uniapp微信小程序实现一个页面多个倒计时
2020/11/01 Javascript
[02:19]2018年度DOTA2最佳核心位选手-完美盛典
2018/12/17 DOTA
python通过post提交数据的方法
2015/05/06 Python
python用pickle模块实现“增删改查”的简易功能
2017/06/07 Python
python如何为创建大量实例节省内存
2018/03/20 Python
对python cv2批量灰度图片并保存的实例讲解
2018/11/09 Python
python爬虫获取新浪新闻教学
2018/12/23 Python
django foreignkey(外键)的实现
2019/07/29 Python
wxPython实现画图板
2020/08/27 Python
浅谈Python的方法解析顺序(MRO)
2020/03/05 Python
python如何从键盘获取输入实例
2020/06/18 Python
python代码能做成软件吗
2020/07/24 Python
使用Html5 Stream开发实时监控系统
2020/06/02 HTML / CSS
马来西亚太阳镜、眼镜和隐形眼镜网上商店:Focus Point
2018/12/13 全球购物
介绍一下Linux中的链接
2016/05/28 面试题
J2EE的优越性主要表现在哪些方面
2016/03/28 面试题
自强之星事迹材料
2014/05/12 职场文书
HR在给员工开具离职证明时,需要注意哪些问题?
2019/07/03 职场文书
html form表单基础入门案例讲解
2021/07/15 HTML / CSS