ExtJS精华函数解析(仅个人见解)

One:    Ext的最先开始的精华函数要算Ext.namespace,它采取的是命名空间的方式.从而可以避免各种变量啊.对象的冲突问题

Two:    Ext的apply采取的是对象继承方式,Ext.apply(o,c,object),既o继承c的所有属性.并且o也拥有object的一切属性

Three   Ext的applyIf采取的对象继承方式,这个跟上面的继承有一点不同,Ext.applyIf(o,c)就是只有当o里的对象不存在时才继承.反之就不继承.

Four:   Ext的extend,这个也是整个Ext的精华所在,所有的对象继承,扩展都是根据Ext.extend的方法而来.本人写的一个htmlEditor也才是看懂了extend方法才搞清楚如何写扩展.
        htmlEditor表情扩展查看地址:http://extjs.com/forum/showthread.php?t=13213    http://extjs.com/learn/Image:Face.jpg.jpg
        Ext.extend(sb, sp, overrides)即sb扩展sp的所有对象,overriders即你想增加的对象或者方法.可以覆盖sp的对象也与方法及属性.



这是本人从ext中扣出来的精华部分,大家完全可以根据这个精华自己去写扩展,写框架.

 

  1. if(typeof(Ext)=="undefined"){
  2.         var Ext={};
  3. }
  4. window["undefined"] = window["undefined"];
  5. Ext.apply = function(o, c, defaults){
  6.     if(defaults){
  7.         Ext.apply(o, defaults);
  8.     }
  9.     if(o && c && typeof c == 'object'){
  10.         for(var p in c){
  11.             o[p] = c[p];
  12.         }
  13.     }
  14.     return o;
  15. };
  16. (function(){
  17.         var ua = navigator.userAgent.toLowerCase();
  18.     var isStrict = document.compatMode == "CSS1Compat",
  19.                 isSafari = (/webkit|khtml/).test(ua),
  20.                 isIE = ua.indexOf("msie") > -1,
  21.         isIE7 = ua.indexOf("msie 7") > -1,
  22.         isGecko = !isSafari && ua.indexOf("gecko") > -1;
  23.         if(isIE && !isIE7){
  24.         try{
  25.             document.execCommand("BackgroundImageCache", false, true);
  26.         }catch(e){}
  27.     }
  28.         Ext.apply(Ext,{
  29.                 emptyFn : function(){},
  30.                 applyIf : function(o, c){
  31.             if(o && c){
  32.                 for(var p in c){
  33.                     if(typeof o[p] == "undefined"){ o[p] = c[p]; }
  34.                 }
  35.             }
  36.             return o;
  37.         },
  38.                 extend : function(){
  39.             var io = function(o){
  40.                 for(var m in o){
  41.                     this[m] = o[m];
  42.                 }
  43.             };
  44.             return function(sb, sp, overrides){
  45.                 if(typeof sp == 'object'){
  46.                     overrides = sp;
  47.                     sp = sb;
  48.                     sb = function(){sp.apply(this, arguments);};
  49.                 }
  50.                 var F = function(){}, sbp, spp = sp.prototype;
  51.                 F.prototype = spp;
  52.                 sbp = sb.prototype = new F();
  53.                 sbp.constructor=sb;
  54.                 sb.superclass=spp;
  55.                 if(spp.constructor == Object.prototype.constructor){
  56.                     spp.constructor=sp;
  57.                 }
  58.                 sb.override = function(o){
  59.                     Ext.override(sb, o);
  60.                 };
  61.                 sbp.override = io;
  62.                 Ext.override(sb, overrides);
  63.                 return sb;
  64.             };
  65.         }(),
  66.                 override : function(origclass, overrides){
  67.             if(overrides){
  68.                 var p = origclass.prototype;
  69.                 for(var method in overrides){
  70.                     p[method] = overrides[method];
  71.                 }
  72.             }
  73.         },
  74.                 namespace : function(){
  75.             var a=arguments, o=null, i, j, d, rt;
  76.             for (i=0; i<a.length; ++i) {
  77.                 d=a[i].split(".");
  78.                 rt = d[0];
  79.                 eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
  80.                 for (j=1; j<d.length; ++j) {
  81.                     o[d[j]]=o[d[j]] || {};
  82.                     o=o[d[j]];
  83.                 }
  84.             }
  85.         },
  86.                 urlEncode : function(o){
  87.             if(!o){
  88.                 return "";
  89.             }
  90.             var buf = [];
  91.             for(var key in o){
  92.                 var ov = o[key], k = encodeURIComponent(key);
  93.                 var type = typeof ov;
  94.                 if(type == 'undefined'){
  95.                     buf.push(k, "=&");
  96.                 }else if(type != "function" && type != "object"){
  97.                     buf.push(k, "=", encodeURIComponent(ov), "&");
  98.                 }else if(ov instanceof Array){
  99.                     if (ov.length) {
  100.                             for(var i = 0, len = ov.length; i < len; i++) {
  101.                                 buf.push(k, "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
  102.                             }
  103.                         } else {
  104.                             buf.push(k, "=&");
  105.                         }
  106.                 }
  107.             }
  108.             buf.pop();
  109.             return buf.join("");
  110.         },
  111.                 urlDecode : function(string, overwrite){
  112.             if(!string || !string.length){
  113.                 return {};
  114.             }
  115.             var obj = {};
  116.             var pairs = string.split('&');
  117.             var pair, name, value;
  118.             for(var i = 0, len = pairs.length; i < len; i++){
  119.                 pair = pairs[i].split('=');
  120.                 name = decodeURIComponent(pair[0]);
  121.                 value = decodeURIComponent(pair[1]);
  122.                 if(overwrite !== true){
  123.                     if(typeof obj[name] == "undefined"){
  124.                         obj[name] = value;
  125.                     }else if(typeof obj[name] == "string"){
  126.                         obj[name] = [obj[name]];
  127.                         obj[name].push(value);
  128.                     }else{
  129.                         obj[name].push(value);
  130.                     }
  131.                 }else{
  132.                     obj[name] = value;
  133.                 }
  134.             }
  135.             return obj;
  136.         },
  137.                 isIE : isIE,
  138.         isIE7 : isIE7,
  139.         isGecko : isGecko,
  140.                 isStrict:isStrict
  141.         });
  142. })();

  143. Ext.applyIf(String, {
  144.     escape : function(string) {
  145.         return string.replace(/('|\\)/g, "\\$1");
  146.     },

  147.     /**
  148.                 var s = String.leftPad('123', 5, '0');
  149.                 // s now contains the string: '00123'

  150.      */
  151.     leftPad : function (val, size, ch) {
  152.         var result = new String(val);
  153.         if(ch === null || ch === undefined || ch === '') {
  154.             ch = " ";
  155.         }
  156.         while (result.length < size) {
  157.             result = ch + result;
  158.         }
  159.         return result;
  160.     },

  161.     /**
  162.                 var cls = 'my-class', text = 'Some text';
  163.                 var s = String.format('<div class="{0}">{1}</div>', cls, text);
  164.                 // s now contains the string: '<div class="my-class">Some text</div>'

  165.      */
  166.     format : function(format){
  167.         var args = Array.prototype.slice.call(arguments, 1);
  168.         return format.replace(/\{(\d+)\}/g, function(m, i){
  169.             return args[i];
  170.         });
  171.     }
  172. });

  173. Ext.applyIf(Array.prototype, {
  174.     indexOf : function(o){
  175.        for (var i = 0, len = this.length; i < len; i++){
  176.               if(this[i] == o) return i;
  177.        }
  178.            return -1;
  179.     },
  180.     remove : function(o){
  181.        var index = this.indexOf(o);
  182.        if(index != -1){
  183.            this.splice(index, 1);
  184.        }
  185.     }
  186. });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值