首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
编程语言
> 详细
javascript鸭式辩型法实现接口
时间:
2016-09-14 01:41:03
阅读:
253
评论:
0
收藏:
0
[点我收藏+]
标签:
[html]
<!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
>
javascript鸭式辩型法实现接口
标签:
原文地址:http://www.cnblogs.com/zhangxinerfenxiang/p/5870277.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
Spring Cloud 从入门到精通(一)Nacos 服务中心初探
2021-07-29
基础的排序算法
2021-07-29
SpringBoot|常用配置介绍
2021-07-29
关于 .NET 与 JAVA 在 JIT 编译上的一些差异
2021-07-29
C语言常用函数-toupper()将字符转换为大写英文字母函数
2021-07-29
《手把手教你》系列技巧篇(十)-java+ selenium自动化测试-元素定位大法之By class name(详细教程)
2021-07-28
4-1 YAML配置文件 注入 JavaBean中
2021-07-28
【python】 用来将对象持久化的 pickle 模块
2021-07-28
马拉车算法
2021-07-28
用Python进行冒泡排序
2021-07-28
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!