js32---CommonUtil.js

來源:互聯網
上載者:User

標籤:ace   命名   父類   tor   handle   com   pre   --   false   

// BH 命名空間  namespacevar BH = {} ;BH.Interface = function(name,methods){  //Interface是類、方法的名字,以後用BH.Interface表示名字。new BH.Interface(‘CarInterface‘ ,[‘start‘,‘run‘]);當類用    //判斷介面的參數個數    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]);    }}// 三:檢驗介面裡的方法BH.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 !== BH.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];            // object[key] 就是方法            if( !object[methodName] || typeof object[methodName] !== ‘function‘ ){                throw new Error("the method name ‘" + methodName + "‘ is not found !");            }        }    }};BH.extend=function(sub ,sup){     var F = new Function();    // 1 建立一個空函數    目的:空函數進行中轉     F.prototype = sup.prototype; // 2 實現空函數的原型對象和超類的原型對象轉換     sub.prototype = new F();     // 3 原型繼承      sub.prototype.constructor = sub ; // 4還原子類的構造器     //儲存一下父類的原型對象: 一方面方便解耦  另一方面方便獲得父類的原型對象     sub.superClass = sup.prototype; //自訂一個子類的靜態屬性 接受父類的原型對象     //判斷父類的原型對象的構造器 (加保險)     if(sup.prototype.constructor == Object.prototype.constructor){         sup.prototype.constructor = sup ; //手動歡迎父類原型對象的構造器     }};/** * 單體模式 * 實現一個跨瀏覽器的事件處理常式。一個冒泡事件一個捕獲事件。 */BH.EventUtil = {    addHandler:function(element , type , handler){        if(element.addEventListener){        //FF            element.addEventListener(type,handler,false);//false表示冒泡        } else if(element.attachEvent){        //IE            element.attachEvent(‘on‘+type , handler);        }    } ,     removeHandler:function(element , type , handler){        if(element.removeEventListener){        //FF            element.removeEventListener(type,handler,false);        } else if(element.detachEvent){        //IE            element.detachEvent(‘on‘+type , handler);        }            }};Array.prototype.each = function(fn){    try{        this.i || (this.i=0);  //var i = 0 ;        if(this.length >0 && fn.constructor == Function){            while(this.i < this.length){    //while迴圈的範圍                 var e = this[this.i];                if(e && e.constructor == Array){                    e.each(fn);                } else {                    //fn.apply(e,[e]);                    fn.call(e,e);                }                this.i++ ;            }            this.i = null ; // 釋放記憶體 記憶體回收機制回收變數        }    } catch(ex){        // do something     }    return this ;}

 

js32---CommonUtil.js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.