作家
登录

JavaScript接口实现代码 (Interfaces In JavaScript)

作者: 来源:www.28hudong.com 2013-03-30 01:31:21 阅读 我要评论

在实际中,我们可以在注释中定义好接口,在实际的代码中予以实现 比如: 复制代码 代码如下: /* 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."); } } } };

  推荐阅读

  js客户端快捷键管理类的较完整实现和应用

复制代码 代码如下: //一个快捷键对象 function KeyOne(id, keys, dom, isfun, fun,iskeydown) { this.id = id; this.keys = keys; this.dom = dom; this.isfun = isfun; this.fun = fun; this.isKeydown = iskeyd>>>详细阅读


本文标题:JavaScript接口实现代码 (Interfaces In JavaScript)

地址:http://www.17bianji.com/kaifa2/JS/26214.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)