JavaScript设计模式学习——Basic Function

来源:互联网 发布:删除数据库重复数据 编辑:程序博客网 时间:2024/06/06 20:24

Interface:

 

var Interface = function(name, methods) {
    if(arguments.length != 2) {
        throw new Error("Interface constructor called with " + arguments.length
          + "arguments, but expected exactly 2.");
    }
   
    this.name = name;
    this.methods = [];
    for(var i = 0, len = methods.length; i < len; 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]);       
    }   
};   

// Static class method.

Interface.ensureImplements = function(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++) {
        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, methodsLen = interface.methods.length; j < methodsLen; j++) {
            var method = interface.methods[j];
            if(!object[method] || typeof object[method] !== 'function') {
                throw new Error("Function Interface.ensureImplements: object "
                  + "does not implement the " + interface.name
                  + " interface. Method " + method + " was not found.");
            }
        }
    }
};

 

=========================================


extend:

 

function extend(subClass, superClass) {
  var F = function() {};
  F.prototype = superClass.prototype;
  subClass.prototype = new F();
  subClass.prototype.constructor = subClass;

  subClass.superclass = superClass.prototype;
  if(superClass.prototype.constructor == Object.prototype.constructor) {
    superClass.prototype.constructor = superClass;
  }
}

 

===========================================

 

$:

 

(function(){
    function _$(els){
        this.elements=[];
        for(var i=0,len=els.length;i<len;i++){
            var element=els[i];
            if(typeof element === "string"){
                element=document.getElementById(element);
            }
            this.elements.push(element);
        }
    }
   
    _$.prototype={
        each:function(fn){
            for(var i=0,len=this.elements.length;i<len;i++){
                fn.call(this,this.elements[i]);
            }
            return this;
        },
       
        setStyle:function(prop,val){
            this.each(function(el){
                el.style[prop]=val;
            });
            return this;
        },
       
        show:function(){
            var that=this;
            this.each(function(el){
                that.setStyle('display','block');
            })
            return this;
        },
       
        addEvent:function(type,fn){
            var add=function(el){
                if(window.addEventListener){
                    el.addEventListener(type,fn,false);
                }
                else if(window.attachEvent){
                    el.attachEvent("on"+type,fn);
                }
            };
            this.each(function(el){
                add(el);
            });
            return this;
        }
    };
   
    window.installHelper=function(scope,name){
        scope[name]=function(){
            return new _$(arguments);
        }
    }

   
})();

原创粉丝点击