標籤: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