js中的接口
来源:互联网 发布:苏州cae软件培训 编辑:程序博客网 时间:2024/05/02 20:29
首先理解一下在js中一切都是对象, 这句话的主要意思是针对以下代码,函数也可以是对象
function display(message){ alert(message) } //可以给函数赋予属性 display.name = 'display';
所以这里以下提到的类、构造函数,实质上是对象。下面进入正题
利用注释模仿接口
接口描述了一个对象应该具有哪些方法,然而js并没有interface这个关键词。在js中模仿接口有几种方式,第一个方式是注释:
/* interface A(){ function add(child); function remove(child); function getChild(index); } interface B(){ function save(); } */ var AB = function(id, method, action){ ... } //对象AB实现了接口A和B AB.prototype.add = function(child){ } AB.prototype.remove = function(child){ } AB.prototype.getChild = function(index){ } AB.prototype.save = function(){ }
这样的方法实现接口,对接口实现的程度完全依赖于个人,同时也没有通过报错来实现测试和调试。所以,第二种方法:用属性检查模仿接口
用属性检查模仿接口
/* interface A(){ function add(child); function remove(child); function getChild(index); } interface B(){ function save(); } */ var AB = function(id, method, action){ this.interfaces = ['A', 'B']; } function check(newInstance){ if(!implements(newInstance, 'A', 'B')){ throw new Error('Object does not implement a instance'); } } function implements(object){ //注意这里从1开始,跳过了object参数 for(var i = 1; i < arguments.length; i++){ var interfaceName = arguments[i]; var interfaceFound = false; for(var j = 0; j < object.interfaces.length; j++){ if(object.interfaces[i] == interfaceName){ interfaceFound = true; break; } } //如果有一个接口没找到,返回错误 !interfaceFound ? return false : '' } return true; }
AB宣称自己实现A和B两个接口,做法是把接口A和B的名称加入interfaces数组,显式声明在对象属性中,并提供了check函数进行检查。这种方法的优点是实现接口提供了文档说明,如果接口不在类/对象所支持的接口内,会看到错误信息,便于监控。但缺点在于,接口的方法是否真正实现,我们并不知道
用鸭式辨型模仿接口
与第二种方法相反,第三种方法认为,类是否声明自己支持哪些接口并不重要,只要他具有接口中的方法就行,因为“像鸭子一样走路并且嘎嘎叫的就是鸭子”。
//interfaces var A = new Interface('A', ['add', 'remove', 'getChild']); var B = new Interface('B', ['save']); var AB = function(id, method, action){ //implements A, B } function check(newInstance){ Interface.ensureImplements(newInstance, A, B); }
check函数承担了验证作用,而验证的核心函数是Interface类上的ensureImplements方法。下面看一下Interface类
var Interface = function(name, methods){ //参数个数检查 if(arguments.length !=2 ){ return new Error('Interface constructor should be called with 2 arguments,but is calling with' + arguments.length + 'arguments') } this.name = name; this.method = []; for(var i = 0, len = methods.length; i < len; i++){ if(typeof method[i] !== 'string'){ //方法名字检查 throw new Error('Interface constructor expects method names to be a string') } this.methods.push(methods[i]) } } //不需要被继承,所以这个方法写在对象上 Interface.ensureImplements = function(object){ if(arguments.length < 2) throw new Error('...'); for(var i = 1, len = arguments.length; i < len; i++){ var interface = arguments[i]; if(interface.constructor !== Interface) { throw new Error('...'); } for(var j = 0, methodsLen = interface.methods.length; j < methodsLen; j++){ var method = interface.method[j]; //如果类没有接口上的方法,或者说类上的方法不是函数,抛出错误 if(!object[method] || typeof object[method] !== 'function'){ throw new Error('...'); } } } }
Interface类对参数有严格的检查,如果check方法没有抛出错误,那么接口已经得到了正确的声明和实现
依赖于接口的设计模式
1.工厂模式。使用接口可以通过工厂返回的对象可以互换使用
2.组合模式。如果不适用接口就无法使用这个模式,组合模式的核心在于将对象群体和其组成对象同等对待,这是通过它们实现同样的接口做到的。
3.装饰者模式。装饰者通过对另一个对象进行包装而发挥作用,这是通过实现与另一个对象相同的接口做到的。
4.命令模式。所有命令对象都要实现同一批方法,通过使用接口可以实现
- js中的接口
- Node.js中的模块接口module.exports
- JS调用ATL COM中的C++接口的做法
- Node.js中的模块接口module.exports浅析
- Node.js中的模块接口module.exports浅析
- Node.js中的模块接口module.exports浅析
- 接口中的接口,醉了
- 携程网JS接口
- 微信JS接口
- 微信JS接口
- 微信JS接口
- js handleEvent接口学习
- 微信JS接口
- 微信JS接口
- 微信JS接口
- 实用JS代码接口
- js接口鸭式辨型法实现
- js天气接口
- JS的prototype和__proto__
- a++和++a的区别
- leetcode_middle_1_338. Counting Bits
- MySQL--连接
- 微信小程序里面的单步调试和变量查看
- js中的接口
- 贵州汇森科技有限责任公司技术支持
- POJ1006 Biorhythms
- maven构建springMVC环境
- zookeeper集群搭建
- Tornado支持http头验证
- Concurrent.futures vs Multiprocessing
- c语言数组实现ArrayList部分函数
- Go语言操作json