-
javascript鸭式辩型法实现接口
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script charset="UTF-8" type="text/javascript">
- /**
- * 创建接口类
- */
-
- function Interface(name,methods){
- if(arguments.length<2){
- throw new Error('需要传递两个参数');
- }
- this.name = name;
- this.methods = [];
- for(var i = 1;i<methods.length;i++){
- var methodName = methods[i];
- if(typeof methodName !=='string'){
- throw new Error('方法要为字符串类型');
- }
- this.methods.push(methodName);
- }
- }
-
- var CompositeInterface = new Interface('CompositeInterface',['add','remove']);
- var FormItemInterface = new Interface('FormItemInterface',['select','update']);
- /**
- * 创建实现类
- */
- function MyImpl(){
-
- }
-
- /**
- * 实现接口
- */
- MyImpl.prototype.add = function(){
- alert('add...');
- }
-
- MyImpl.prototype.remove = function(){
- alert('remove...');
- }
- MyImpl.prototype.select = function(){
- alert('select...');
- }
- // MyImpl.prototype.update = function(){
- // alert('update...');
- // }
-
- /**
- * 检测是否实现接口中的所有方法
- */
- Interface.ensureImplements = function(object){
- if(arguments.length<2){
- throw Error('参数个数不能小于2个');
- }
- for(var j=1;j<arguments.length;j++){
- var interfaceInstance = arguments[j];
- if(interfaceInstance.constructor !==Interface){
- throw new Error(interfaceInstance+'不是所需接口实例');
- }
-
-
- for(var i = 0;i<interfaceInstance.methods.length;i++){
- var methodName = interfaceInstance.methods[i];
- if( !object[methodName] || typeof object[methodName] !=='function'){
- throw new Error(methodName+'不是函数或没有被实现');
- }
- }
- }
- }
-
- var c1 = new MyImpl();
- Interface.ensureImplements(c1,CompositeInterface,FormItemInterface);
- c1.add();
- </script>
- </head>
- <body>
- </body>
- </html>
-
相关阅读:
Drop goldengate用户时报ORA-00604 ORA-20782 ORA-06512问题解决
如何查看机器品牌型号
ORA-28040:没有匹配的验证协议
oracle 12C CDB下开启wallet
mysql备份和恢复
手工模拟vip切换
半同步复制的安装部署
linux下安装mysql5.7.11(二进制方式)
postgrep创建存储过程例子
Nginx 是前端工程师的好帮手
-
原文地址:https://www.cnblogs.com/zhangxinerfenxiang/p/5870277.html
Copyright © 2020-2023
润新知