[html] 
 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  2. "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6.         <script charset="UTF-8" type="text/javascript">  
  7.             /**  
  8.              * 创建接口类  
  9.              */  
  10.               
  11.             function Interface(name,methods){  
  12.                 if(arguments.length<2){  
  13.                     throw new Error('需要传递两个参数');  
  14.                 }  
  15.                 this.name = name;  
  16.                 this.methods = [];  
  17.                 for(var i = 1;i<methods.length;i++){  
  18.                     var methodName = methods[i];  
  19.                     if(typeof methodName !=='string'){  
  20.                         throw new Error('方法要为字符串类型');  
  21.                     }  
  22.                     this.methods.push(methodName);  
  23.                 }  
  24.             }  
  25.               
  26.             var CompositeInterface = new Interface('CompositeInterface',['add','remove']);  
  27.             var FormItemInterface = new Interface('FormItemInterface',['select','update']);  
  28.             /**  
  29.              * 创建实现类  
  30.              */  
  31.             function MyImpl(){  
  32.                   
  33.             }  
  34.               
  35.             /**  
  36.              * 实现接口  
  37.              */  
  38.             MyImpl.prototype.add = function(){  
  39.                 alert('add...');  
  40.             }  
  41.               
  42.             MyImpl.prototype.remove = function(){  
  43.                 alert('remove...');  
  44.             }  
  45.             MyImpl.prototype.select = function(){  
  46.                 alert('select...');  
  47.             }  
  48.             // MyImpl.prototype.update = function(){  
  49.                 // alert('update...');  
  50.             // }  
  51.               
  52.             /**  
  53.              * 检测是否实现接口中的所有方法  
  54.              */  
  55.             Interface.ensureImplements = function(object){  
  56.                 if(arguments.length<2){  
  57.                     throw Error('参数个数不能小于2个');  
  58.                 }  
  59.                 for(var j=1;j<arguments.length;j++){  
  60.                     var interfaceInstance = arguments[j];  
  61.                     if(interfaceInstance.constructor !==Interface){  
  62.                         throw new Error(interfaceInstance+'不是所需接口实例');  
  63.                     }  
  64.                   
  65.                   
  66.                     for(var i = 0;i<interfaceInstance.methods.length;i++){  
  67.                         var methodName = interfaceInstance.methods[i];  
  68.                         if( !object[methodName] || typeof object[methodName] !=='function'){  
  69.                             throw new Error(methodName+'不是函数或没有被实现');  
  70.                         }  
  71.                     }  
  72.                 }  
  73.             }  
  74.               
  75.             var c1 = new MyImpl();  
  76.                 Interface.ensureImplements(c1,CompositeInterface,FormItemInterface);  
  77.                 c1.add();  
  78.         </script>  
  79.     </head>  
  80.     <body>  
  81.     </body>  
  82. </html>  

 

posted on 2016-09-14 00:02  web前段领域  阅读(197)  评论(0编辑  收藏  举报