JavaScript Interface 接口的实现

小小子,simaopig 2010-07-12 23:26:03 累计浏览 7,373 次

    JavaScript是弱类型语言,所以类型匹配问题很难追踪。同时,Js并没有像其他语言一样,提供内置的创建或实现接口的方法。这样,在我们进行对象转化的时候是很困难的。

    不过,我们还是可以使用程序来模拟JavaScript Interface接口的实现。一般来说,模拟Interface的实现有如下三种方法:

  1. 注释法――将接口的定义写在注释中,大家能否小心注意,以实现接口的所有方法全凭自觉属性
  2. 检查法――自己说明我实现了哪些接口,一会儿你检查的时候记得检查我说我实现的接口里面,是否把我真正想要实现的接口全部实现了(这么别扭呢),总之就是自欺欺人
  3. 鸭式辨型法――像鸭子一样,嘎嘎叫的那么就是鸭子。具有鸭子特性的东西,我们就可以称之为鸭子。能直立行走,会使用工具的除了机器人,都是人。嗯~~

    按照书中介绍的,我们也一样彩注释法与鸭式辨型相结合的方法,来模拟Js的接口实现。

// Constructor 
/* 
 * @param name String 接口的名字 
 * @param methods Array 接口里面定义的方法 
*/ 
var Interface = function(name, methods){ 
    //如果购造函数的参数不等于2个,那么抛出异常 
    if (arguments.length != 2) { 
        throw new Error("Interface constructor called with " + arguments.length + 
        "arguments,but expected exactyl 2.") 
    } 

    this.name = name; 
    this.methods = []; 
    //方法数组,保证传进来的methods数组中,每一个元素都是字符串类型 
    for (var i = 0, len = methods.length; i < len; i++) { 
        if (typeof methods[i] !== "string") { 
            throw new Error("Interface constructor expects methods names to bo " + 
            "passed in asastring."); 
        } 
        this.methods.push(methods[i]); 
    } 
} 

//Static class methods 
Interface.ensureImplements = function(object){ 
        //如果参数少于2个,抛出异常,object是待判断实现接口的对象 
        if (arguments.length < 2) { 
                throw new Error("Function Interface.ensureImplements called with " + arguments.length + 
                "arguments,but expected at least 2."); 
        } 
        for (var i = 1, len = arguments.length; i < len; i++) { 
                //inter_face为接口,一定要实现Interface类 
                //书中使用interface,因是JavaScript中保留字,所以暂替换为inter_face 
                var inter_face = arguments[i]; 
                if (inter_face.constructor !== Interface) { 
                        throw new Error("Function Interface.ensureImplementsexpects arguments " + 
                        "two and above to be instances of Interface."); 
                } 
                for (var j = 0, methodsLen = inter_face.methods.length; j < methodsLen; j++) { 
                        //对象中是否含有接口中定义的方法 
                        var method = inter_face.methods[j]; 
                        if (!object[method] || typeof object[method] !== ’function’) { 
                                throw new Error("Function Interface.ensureImplements: object " + 
                                "does not implements the " + 
                                inter_face.name + 
                                "interface.Method " + 
                                method + 
                                "was not found."); 
                        }

    可能大家比较晕,注释写在哪了?答,注释写在调用的页面上。例如:

//定义接口Composite,实现add,remove,getChild三种方法 
var Composite = new Interface(’Composite’,[’add’,’remove’,’getChild’]); 

//定义接口FormItem,实现save方法 
var FormItem = new Interface(’FormItem’,[’save’]); 

//判断对象是否实现了上述两个接口 
var object = new Class(); 
Interface.ensureImplements(object,Composite,FormItem);
    接口固然是好用,不过有的时候也要判断一下是否有必要用接口。这可是一项很难的工作啊。具体情况具体分析,决定权在你。

同分类推荐文章

  1. Embracing Playful Imperfection With The CSS `random()` Function (2026-11-14 03:10:02)
  2. Document Picture-in-Picture API与任意元素画中画实现 (2026-11-13 17:10:02)
  3. 给博客做个站内全文搜索:纯前端离线倒排索引的设计与取舍 (2026-10-03 16:40:31)

查看更多 前端 文章 →

建议继续学习

  1. JQuery实现Excel表格呈现 (累计阅读 48,734)
  2. 深入理解Javascript之执行上下文(Execution Context) (累计阅读 18,922)
  3. 从输入 URL 到页面加载完成的过程中都发生了什么事情? (累计阅读 16,397)
  4. 图片动态局部毛玻璃模糊效果的实现 (累计阅读 15,180)
  5. 天朝第二代身份证号码的验证机制 (累计阅读 15,106)
  6. HTML 5 的data-* 自定义属性 (累计阅读 14,741)
  7. 分享一个JQUERY颜色选择插件 (累计阅读 14,550)
  8. 什么是全栈工程师? (累计阅读 14,414)
  9. 快速排序(Quicksort)的Javascript实现 (累计阅读 12,102)
  10. 7 天打造前端性能监控系统 (累计阅读 11,782)