js实现接口的方式

22 篇文章 0 订阅
3 篇文章 0 订阅

js没有interface关键字,怎么实现接口呢,可以用prototype
/*
* interface Composite {
* function add(obj);
* function remove(obj);
* function update(obj);
* }
*/

// CompositeImpl implements Composite
 var  CompositeImpl = function () {

 };
 CompositeImpl.prototype.add = function (obj) {
     
 }
 CompositeImpl.prototype.remove = function (obj) {
     
 }
 CompositeImpl.prototype.update = function (obj) {
     
 }

//这种方式的优点:简单明了,给程序员一个参考,程序员可以参考模板继续往下添加方法
//缺点: 通过注释来声明,这个属于文档规范的范畴,需要程序员严格遵守约定
/**java实现形式

  • public interface Walkable {
  •    public void walk();
    
  • }
  • public interface Fightable {
  •    public void fight();
    
  • }
    */
    var Knight = function(name) {
    this.name = name;
    //Knight实现了Walkabe和Fightable
    };
    Knight.prototype.walk = function() {
    alert(this.name+" is walking!");
    };
    Knight.prototype.fight = function() {
    alert(this.name+" is fighting!");
    };
    var k = new Knight(“jay”);
    k.fight();
    2.基于属性
    /**这种方式的优点:实现了检查接口是否完全被实现
  • 缺点:这种属于文档的范畴,但接口的方法未检测
  • (假如我说明了this.implementInterfaces=[“Walkable”,“Fightable”];
  • 但是我没有真正定义Walkable和Fightable接口中的方法(没实现),他一样不会报错)
  • public interface Walkable {
  •    public void walk();
    
  • }
  • public interface Fightable {
  •    public void fight();
    
  • }
    */
    var Knight = function(name) {
    this.name = name;
    //implementInterfaces是固定的(说明实现了那些对象)
    this.implementInterfaces=[“Walkable”,“Fightable”];
    };
    Knight.prototype.walk = function() {
    alert(this.name+" is walking!");
    };
    Knight.prototype.fight = function() {
    alert(this.name+" is fighting!");
    };
    //检查是否实现了上面定义的this.implementInterfaces=[“Walkable”,“Fightable”]接口数组
    function CheckKnight(obj) {
    if(!isImplements(obj,“Fightable”,“Walkable”)) throw new Error(“必须实现Fightable和Walkable两个接口!”);
    };
    function isImplements(obj) {
    //obj为要验证得对象(如果没有实现的接口,就不让它验证)
    if(!obj.implementInterfaces) throw new Error(“必须声明所需要实现的接口”);
    //每一个方法中都存在一个对象arguments来存储传递进来的实际参数
    //除了第一个以外,其他为实现了接口对象
    for(var i=1;i<arguments.length;i++) {
    //判断参数是否为String
    if(typeof arguments[i]!=“string”) throw new Error(arguments[i]+“的类型不正确”);
    var found = false;
    //obj.implementInterfaces.length是上面定义的接口数组
    for(var j=0;j<obj.implementInterfaces.length;j++) {
    var inter = obj.implementInterfaces[j];
    //只要找到相同的字符串就代表实现了相应接口
    if(inter==arguments[i]) {
    found = true;
    break;
    }
    }
    //只要isImplements(obj,“Fightable”,“Walkable”)
    // 后面的字符串(接口对象)有一个没有实现就报错
    if(!found) return false;
    }
    return true;
    };
    var k = new Knight(“zyd”);
    CheckKnight(k);
    k.walk();
  1. 通过鸭式辨认来实现接口:某个类是否声明自己支持哪些接口并不重要,只要它具有接口中的这些方法就行。
    // 3、鸭式辩型法实现接口(最好的javascript实现接口的方式)

//检验接口对象,同时把方法名放进数组中
var Interface = function(name,methods) {
//判断接口的参数个数(第一个为接口对象,第二个为参数数组)
if(arguments.length != 2) {
throw new Error(‘创建的接口对象参数必须为两个,第二个为方法数组’)
}
//接口对象引用名
this.name = name;
//自己的属性
this.methods = [];//定义一个内置的空数组对象 等待接受methods里的元素(方法名称)
//判断数组是否中的元素是否为string的字符串
for (var i = 0 ; i < methods.length; i++) {
//判断方法数组里面是否为string(字符串)的属性
if(typeof methods[i] != ‘string’) {
throw new Error(‘方法名必须是string类型的!’)
}
//把他放在接口对象中的methods中(把接口方法名放在Interface对象的数组中)
this.methods.push(methods[i]);
}
}
//实例化接口对象(用上面那个方法检验)
var Interface1 = new Interface(‘Interface1’,[‘add’,‘delete’]);
var Interface2 = new Interface(‘Interface2’,[‘select’,‘update’]);

var object1 = function () {

}
//定义原型方法,每次new对象时不用再占空间
object1.prototype.add = function () {
alert(‘add something’);
}
object1.prototype.delete = function () {
alert(‘delete something’);
}
object1.prototype.update = function () {
alert(‘update something’);
}
object1.prototype.select = function () {
alert(‘select something’);
}
Interface.checkImplements = function (obj) {
//判断接口的参数个数<2的话,参数传递失败
// (obj的arguments,第一个为需要实现接口的对象,后面的为需要实现的接口)
if(arguments.length < 2) {
throw new Error(‘参数个数必须大于2’);
}
//获得接口实例对象
for (var i = 1 ; i < arguments.length; i++) {
var instanceInterface = arguments[i];//(arguments从下标为1开始全是接口对象)
//判断参数是否是接口类的类型
if(instanceInterface.constructor != Interface) {
throw new Error(‘接口对象不属于Interface检验过的对象!’);
}
//循环接口实例对象里面的每一个方法
///obj[‘a’]

obj.a是相等的
/
for (var j = 0 ; j <instanceInterface.methods.length; j++) {
var methodName = instanceInterface.methods[j];
//判断obj中是否实现了接口的方法和methodName是方法(而不是属性)
if(!obj[methodName] || typeof obj[methodName]!=‘function’) {
throw new Error(‘有接口的方法没实现’)
}
};
}
}
var o1 = new object1();
//此处检验是否实现接口函数 一旦写入了接口就必须实现其中的方法
Interface.checkImplements(o1,Interface1,Interface2);
o1.add();//add something

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端段

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值