<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type=text/javascript charset=utf-8>
//javascript没有定义接口的概念,我们通过模拟高级程序语言的方式来创建javascript中的接口。实现要重写接口所有的方法。
/* javascript定义接口有三种方式
1 注释描述接口
2 属性检测接口
3 鸭式辩型接口
*/ // 1 注解描述的方式(注释方式,假的)
/**
interface Composite {
function add(obj);
function remove(obj);
function uopdate(obj);
} var CompositeImpl = function(){
this.add = function(){},
this.remove = function(){},
this.upload = function(){}
};
var c1 = new CompositeImpl();
var c2 = new CompositeImpl();
alert(c1.add == c2.add); //false,每次都有一个add,所以写到原型里面
*/ // CompositeImpl implements Composite
var CompositeImpl = function(){
}; CompositeImpl.prototype.add = function(obj){
}
CompositeImpl.prototype.remove = function(obj){
}
CompositeImpl.prototype.update = function(obj){
} var c1 = new CompositeImpl();
var c2 = new CompositeImpl();
alert(c1.add == c2.add);
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type=text/javascript charset=utf-8>
// 第二种实现接口的方式 属性检测的方式(继承接口,就要实现所有方法。)
/**
* interface Composite {
* function add(obj);
* function remove(obj);
* function uopdate(obj);
* }
*
* interface FormItem {
* function select(obj);
* }
*
*/ // CompositeImpl implements Composite , FormItem
var CompositeImpl = function(){
// 显示的再类的内部 接受所实现的接口
// 一般来说是一个规范 我们项目经理:在类的内部定义一个数组(名字要固定)
this.implementsInterfaces = ['Composite' ,'FormItem' ];//this是这个类的当前对象
} CompositeImpl.prototype.add = function(obj){
alert('add...');
}
CompositeImpl.prototype.remove = function(obj){
}
CompositeImpl.prototype.update = function(obj){
}
CompositeImpl.prototype.select = function(obj){
} // 检测CompositeImpl类的对象的
function CheckCompositeImpl(instance){
//判断当前对象是否实现了所有的接口
if(!IsImplements(instance,'Composite','FormItem')){
throw new Error('Object does not implement a required interface!');
}
} function IsImplements(object){//实参多个,形参一个,这个形参就是实参的第一个,
// arguments 对象 获得函数的实际参数
for(var i = 1 ; i < arguments.length;i++){
//接受所实现的每一个接口的名字
var interfaceName = arguments[i];//arguments[i]在不在object.implementsInterfaces里面
var interfaceFound = false ;
for(var j = 0 ; j <object.implementsInterfaces.length;j++){
if(object.implementsInterfaces[j] == interfaceName) {
interfaceFound = true ;
break;//跳出for循环
}
}
if(!interfaceFound){
return false ;
}
}
return true ;
} var c1 = new CompositeImpl();
CheckCompositeImpl(c1);
c1.add();
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type=text/javascript charset=utf-8>
// 实现接口的第三种方式:鸭式辨型法实现接口(最完美的javascript实现接口方式)
// 鸭式辨型法实现的核心:一个类实现接口的主要目的:把接口里的方法都实现(检测方法)
// 完全面向对象 代码也实现统一 也解耦了 // 一: 接口类 Class Interface ==>实例化N多个接口 /**
* 接口类需要2个参数
* 参数1: 接口的名字 (string)
* 参数2: 接受方法名称的集合(数组) (array)
*/
var Interface = function(name,methods){
//判断接口的参数个数
if(arguments.length != 2){
throw new Error('this instance interface constructor arguments must be 2 length!');
}
this.name = name ;
this.methods = [] ; //定义一个内置的空数组对象 等待接受methods里的元素(方法名字)
for(var i = 0,len = methods.length ; i <len ; i++){
if( typeof methods[i] !== 'string'){
throw new Error('the Interface method name is error!');
}
this.methods.push(methods[i]);
}
} // 1 实例化接口对象
var CompositeInterface = new Interface('CompositeInterface' , ['add' , 'remove']);//CompositeInterface对象有属性name为CompositeInterface,数组['add' , 'remove']
var FormItemInterface = new Interface('FormItemInterface' , ['update','select']);
// CompositeImpl implements CompositeInterface , FormItemInterface
var CompositeImpl = function(){
}
// 3 实现接口的方法implements methods
CompositeImpl.prototype.add = function(obj){
alert('add');
}
CompositeImpl.prototype.remove = function(obj){
alert('remove');
}
CompositeImpl.prototype.update = function(obj){
alert('update');
}
/*
CompositeImpl.prototype.select = function(obj){
alert('select');
}
*/ // 三:检验接口里的方法
Interface.ensureImplements = function(object){
// 如果检测方法接受的参数小于2个 参数传递失败!
if(arguments.length < 2 ){
throw new Error('Interface.ensureImplements method constructor arguments must be >= 2!');
} for(var i = 1 , len = arguments.length; i<len; i++ ){
var instanceInterface = arguments[i];
// 判断对象是不是一个类的实例,判断对象的构造器是不是类名字
if(instanceInterface.constructor !== Interface){
throw new Error('the arguments constructor not be Interface Class');
}
// 循环接口实例对象里面的每一个方法
for(var j = 0 ; j < instanceInterface.methods.length; j++){
// 用一个临时变量 接受每一个方法的名字(注意是字符串)
var methodName = instanceInterface.methods[j];
if( !object[methodName] || typeof object[methodName] !== 'function' ){
throw new Error("the method name '" + methodName + "' is not found !");
}
}
}
} var c1 = new CompositeImpl();
Interface.ensureImplements(c1,CompositeInterface,FormItemInterface);
c1.add();
</script>
</head>
<body>
</body>
</html>
var CompositeImpl = function(){}
CompositeImpl.prototype.add = function(obj){
alert('add');
} var c1 = new CompositeImpl();
alert(c1['add']);//function(obj){alert('add');}
alert(c1['ssadd']);//undefined
alert(c1['sss']);//undefined,
alert(c1.ssadd());//ssadd is not a function
alert(c1.sss);//undefined, 没有属性,方法,通过中括号返回未定义,就是假
c1['add'];//什么都没有
alert(typeof c1['add']);//function if( !c1[methodName] || typeof c1[methodName] !== 'function' ){
throw new Error("the method name '" + methodName + "' is not found !");
}// c1[methodName]有可能是一个属性,不是方法。 !== // 判断对象是不是一个类的实例,判断对象的构造器是不是类名字
instanceInterface.constructor !== Interface

js20---接口3种方式的更多相关文章

  1. C# 调用HTTP接口两种方式

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...

  2. HBase读写的几种方式(三)flink篇

    1. HBase连接的方式概况 主要分为: 纯Java API读写HBase的方式: Spark读写HBase的方式: Flink读写HBase的方式: HBase通过Phoenix读写的方式: 第一 ...

  3. C#动态调用WCF接口,两种方式任你选。

    写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...

  4. Java创建线程的第二种方式:实现runable接口

    /*需求:简单的卖票程序多个窗口买票 创建线程的第二种方式:实现runable接口 *//*步骤1.定义类实现Runable接口2.覆盖Runable接口中的run方法    将线程要运行的代码存放在 ...

  5. 创建线程的两种方式:继承Thread类和实现Runnable接口

    第一种方式:继承Thread类 步骤:1.定义类继承Thread 2.覆写Threa类的run方法. 自定义代码放在run方法中,让线程运行 3.调用线程的star方法, 该线程有两个作用:启动线程, ...

  6. java 的对象拷贝(有深浅拷贝两种方式,深拷贝实现的两种方式(逐层实现cloneable接口,序列化的方式来实现))

    Java提高篇--对象克隆(复制)(转自:http://www.cnblogs.com/Qian123/p/5710533.html#_label0)   阅读目录 为什么要克隆? 如何实现克隆 浅克 ...

  7. java中创建多线程两种方式以及实现接口的优点

    多线程创建方式有两种 创建线程的第一种方式.继承Thread类 1.继承Thread类 2.重写Thread类中的run方法--目的将自定义代码存储在run方法.让线程执行3.调用线程的start() ...

  8. 8.13.2 TreeSet实现Comparable接口的两种方式

    推荐使用第二种方式,编写比较器可以使数据类的程序耦合度降低,同时比较器也可以重复利用! 第一种方式:数据类实现Comparable接口,实现其中的compareTo方法 创建对象时,使用TreeSet ...

  9. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  10. 51、多线程创建的三种方式之实现Callable接口

    实现Callable接口创建线程 Callable接口是在jdk5版本中加入的,这个接口在java.util.concurrent包下面,与其他两种方式不同的地方在于使用Callable接口创建的线程 ...

随机推荐

  1. 洛谷——P1970 花匠

    https://www.luogu.org/problem/show?pid=1970 题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走, ...

  2. 手动创建DataTable并添加数据

    DataTable dt=new DataTable(); DataColumn dc=dt.Columns.Add("OBJECTID",Type.GetType("S ...

  3. Android——bootchart

    bootchart:android原生自带的开机性能查看机制.通过收集android开机过程中的各种log数据,终于能够图表的形式展现各个进程在开机过程中的性能.(博客不能断-) 撰写不易,转载需注明 ...

  4. Dojo入门篇

    Dojo是一个JavaScript实现的开源DHTML工具包,Dojo最初的目标是解决开发HTML应用程序中遇到的一些长期存在的问题.然而如今Dojo已经成为了开发RIA应用程序的利器. Dojo让W ...

  5. CSS元素选择器 element selector(type selector)

    http://www.w3school.com.cn/css/css_selector_type.asp 元素选择器 最常见的 CSS 选择器是元素选择器.换句话说,文档的元素就是最基本的选择器. 如 ...

  6. 智课雅思词汇---六、fer是什么意思

    智课雅思词汇---六.fer是什么意思 一.总结 一句话总结:词根:fer = to carry(拿), to bring(带来), to bear(负担, 1.equ是什么意思? 词根:-equ- ...

  7. 基于 Web 的 Go 语言 IDE - Wide 1.2.0 发布!

    Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE. 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈.Lint 实时运行:极速编译. ...

  8. Javascript 模块化理解

    原始时代: script标签引入javascript文件 -------- html ------- <div id="result"></div> < ...

  9. 参考《机器学习实战》高清中文PDF+高清英文PDF+源代码

    机器学习是人工智能研究领域中一个极其重要的研究方向,在现今的大数据时代背景下,捕获数据并从中萃取有价值的信息或模式,成为各行业求生存.谋发展的决定性手段,这使得这一过去为分析师和数学家所专属的研究领域 ...

  10. Python协程一点理解

    协程,又称微线程,纤程.英文名Coroutine. 线程是系统级别的它们由操作系统调度,而协程则是程序级别的由程序根据需要自己调度.在一个线程中会有很多函数,我们把这些函数称为子程序,在子程序执行过程 ...