ExtJs UI框架學習四

透析Extjs的Ext.js源碼(五)分析繼承的實現

關鍵字: extjs ext中有關繼承的實現的關鍵代碼如下:(Ext.js中)



extend:


Js代碼
  1. extend : function(){  
  2.             // inline overrides  
  3.             var io = function(o){  
  4.                 for(var m in o){  
  5.                     this[m] = o[m];  
  6.                 }  
  7.             };  
  8.             var oc = Object.prototype.constructor;  
  9.               
  10.             return function(sb, sp, overrides){  
  11.                 if(typeof sp == 'object'){  
  12.                     overrides = sp;  
  13.                     sp = sb;  
  14.                     sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};  
  15.                 }  
  16.                 var F = function(){}, sbp, spp = sp.prototype;  
  17.                 F.prototype = spp;  
  18.                 sbp = sb.prototype = new F();  
  19.                 sbp.constructor=sb;  
  20.                 sb.superclass=spp;  
  21.                 if(spp.constructor == oc){  
  22.                     spp.constructor=sp;  
  23.                 }  
  24.                 sb.override = function(o){  
  25.                     Ext.override(sb, o);  
  26.                 };  
  27.                 sbp.override = io;  
  28.                 Ext.override(sb, overrides);  
  29.                 sb.extend = function(o){Ext.extend(sb, o);};  
  30.                 return sb;  
  31.             };  
  32.         }()  


override:


Js代碼
  1. override : function(origclass, overrides){  
  2.             if(overrides){  
  3.                 var p = origclass.prototype;  
  4.                 for(var method in overrides){  
  5.                     p[method] = overrides[method];  
  6.                 }  
  7.             }  
  8.         }  


Ext.apply:


Js代碼
  1. Ext.apply = function(o, c, defaults){  
  2.     if(defaults){  
  3.         // no "this" reference for friendly out of scope calls  
  4.         Ext.apply(o, defaults);  
  5.     }  
  6.     if(o && c && typeof c == 'object'){  
  7.         for(var p in c){  
  8.             o[p] = c[p];  
  9.         }  
  10.     }  
  11.     return o;  
  12. };  


最關鍵的是extend部分的代碼,它使得如果子類沒有constructor(或者說子類的constructor就是個默認的 Object),那麼當new一個子類的時候,會調用他的父類的構造器,因此,我們看到Panel是直接通過Ext.Panel = Ext.extend(Ext.Contailer,{...});的方式直接定義的,在new Panel({...})的時候就能層層進去一直到有構造器的超類Ext.Component,並執行這個超類的構造器裏的代碼。而 Ext.Component的構造器代碼中有this.initComponet()方法,這樣就能夠調用子類的initComponent()方法,而 子類的initComponent()方法中都有 子類名.superclass.initComponent();這樣的代碼來調用父類的init方法,這樣我們在new一個子類的後就能夠直接初始化了 所有的信息。


extend中最關鍵的一句話是:


sb = overrides.constructor != oc ? overrides.constructor :

function(){sp.apply(this, arguments);}

;


它表示了執行父類構造器的原因,


new對象時,就是執行這個function(){sp.apply(this, arguments);}方法,


sp.applay()執行時,父類構造器就會馬上被執行。




比較並執行下面的代碼就可以理解了上面說的內容了:



1、子類有構造器的情況


Js代碼
  1. Parent = function() {  
  2.     alert("parent");  
  3. };  
  4. Child = function() {  
  5.     alert("child");  
  6. };  
  7. Ext.extend(Child, Parent, {  
  8.     init : function() {  
  9.         alert("child's init function");  
  10.     }  
  11. });  
  12. var cl = new Child();// 輸出結果:child  
  13. cl.init();// 輸出結果:child's init function  


2、子類沒有構造器的情況


Js代碼
  1. Parent = function() {  
  2.     alert("parent");  
  3. };  
  4. Child = Ext.extend(Parent, {  
  5.     init : function() {  
  6.         alert("child's init function");  
  7.     }  
  8. });  
  9. var cl = new Child();// 輸出結果:parent  
  10. cl.init();// 輸出結果:child's init function 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章