asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0
    var Interface = function(name, methods){
       if(arguments.length != 2){
          throw new Error("Interface constructor called with " + arguments.length + " argument, but expected exactly 2.");
       }
       this.name = name;
       this.methods = [];
       for(var i = 0, l =  methods.length; i < l; i++){
            if(typeof methods[i] !== "string"){
                throw new Error("Interface constructor expects method names to be passed in as a string");
            }
            this.methods.push(methods[i]);
       }
    }
    Interface.ensureImplements = function(obj){
        if(arguments.length < 2){
            throw new Error("Function Interface.ensureImplements called with " +
arguments.length + " argument, but expected at least 2.");
        }
        for(var i = 1, l =  arguments.length; i < l; i++){
           var interface = arguments[i];
           if(interface.constructor !== Interface){
               throw new Error("Function Interface.ensureImplements expects arguments two and above to be instances of Interface.");
           }
           for(var j = 0, len = interface.methods.length ; j < len; j++){
                var method = interface.methods[j];
                if(!obj[method] || typeof obj[method] !== 'function'){
                     throw new Error("Function Interface.ensureImplements: object "
+ "does not implement the " + interface.name
+ " interface. Method " + method + " was not found.");
                }
           }
        }
    }
It might seem like interfaces reduce JavaScript’s flexibility, but they actually improve it by allowing
your objects to be more loosely coupled.

posted on 2009-03-16 21:42 汪杰 阅读(251) 评论(0)  编辑 收藏 引用 所属分类: javascript
只有注册用户登录后才能发表评论。

<2006年10月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 466814
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜