javascript接口

实现接口的三种方法

1、注解描述的方法

 /*
             * interface Compose{
             *     function add(obj);
             *     function remove(obj);
             *     function update(obj);
             * 
             * }
             */

优点:以注释的方式规范接口中的方法

缺点:实际上接口并不存在,只是文档的范畴,没有检查接口是否被完全实现

2、属性检测方法

在类的内部定义一个数组,(名字固定),数组内容为要实现的接口,要创建一个检测方法,判断当前对象是否实现了所有接口。

缺点:只是检测接口,并没有检测接口中的方法

 /*
              * interface Comp{
              *     this.add(obj);
              *     this.update(obj);
              *     this.remove(obj);
              * }
              * interface Item{
              *     this.select(obj);
              * }
              */
             var CompItem=function(){
                 this.inter=['Comp','Item'];
             };
             CompItem.prototype.add=function(obj){
                 alert('add');
             };
             CompItem.prototype.remove=function(obj){
                 alert('remove');
             };
             CompItem.prototype.update=function(obj){
                 alert('update');
             };
             CompItem.prototype.select=function(obj){
                 alert('select');
             };
             function check(obj){
                 if(!hasImplements(obj,'Comp','Item')){
                     throw new Error('There are Interface');
                 }
             }
             function hasImplements(obj){
                 //实参都记录在arguments中
                 for(var i=1;i<arguments.length;i++){
                     var attribute=arguments[i];
                     var value=false;
                     for (var j=0; j < obj.inter.length; j++) {
                         //与类中定义的数组中的属性比较
                       if(attribute==obj.inter[j]){
                           value=true;
                           break;
                       }
                     }
                     if(!value)
                     return false;
                 }
                 return true;
             }
             var a=new CompItem();
             check(a);


3、鸭式辩型法

创建接口类,传递:接口名、接口方法

实例化接口

创建具体类,并实现接口中的方法

检验当前类的对象是否实现了接口,检验不通过,抛出异常

//创建一个接口类,传递两个参数
           var Interface=function(name,method){
               if(arguments.length!=2){
                   throw new Error('接口中参数个数不对');
               }
               this.name=name;
               this.methods=[];
               for (var i=0; i < method.length; i++) {
                   if(typeof method[i]!='string'){
                       throw new Error('接口中第二个参数不是字符串数组');
                   }
                 this.methods.push(method[i]);
               };
           };
         //创建一个普通类,用来实现接口 
         var Class=function(){
             
         };
         //在类中添加方法
         Class.prototype.add=function(){};
         Class.prototype.remove=function(){};
         Class.prototype.update=function(){};
         Class.prototype.select=function(){};
         //实例化两个接口对象A,B
         var A=new Interface('A',['add','remove']);
         var B=new Interface('B',['update','select']);
         //实例化一个类,类中实现了接口中的方法
         var c=new Class();
         //检验类是否实现了接口中全部方法
         Interface.check=function(object){
             if(arguments.length<2){
                 throw new Error('方法中传递的参数个数小于2');
             }
             for (var i=1; i < arguments.length; i++) {
               var itf=arguments[i];
               for (var j=0; j < itf.methods.length; j++) {
                 var mtd=itf.methods[j];
                 if(!object[mtd]||typeof object[mtd]!='function'){
                     throw new Error('对象中不存在接口中的方法');
                 }
               };
             };
         };
         Interface.check(c,A,B);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值