ITeye博客

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

sailorlee · 2008-12-29 11:44 · ExtJS · C · C++ · EXT · C# · prototype · Markdown

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. /*
  2. *
  3. * Author:Sloppy
  4. * Date:2007-09-15
  5. * Contact:QQ:35734786
  6. */
  7. if(typeof(Ext)=="undefined"){
  8. var Ext={};
  9. }
  10. window["undefined"] = window["undefined"];
  11. Ext.apply = function(o, c, defaults){
  12. if(defaults){
  13. Ext.apply(o, defaults);
  14. }
  15. if(o && c && typeof c == 'object'){
  16. for(var p in c){
  17. o[p] = c[p];
  18. }
  19. }
  20. return o;
  21. };
  22. (function(){
  23. var ua = navigator.userAgent.toLowerCase();
  24. var isStrict = document.compatMode == "CSS1Compat",
  25. isSafari = (/webkit|khtml/).test(ua),
  26. isIE = ua.indexOf("msie") > -1,
  27. isIE7 = ua.indexOf("msie 7") > -1,
  28. isGecko = !isSafari && ua.indexOf("gecko") > -1;
  29. if(isIE && !isIE7){
  30. try{
  31. document.execCommand("BackgroundImageCache", false, true);
  32. }catch(e){}
  33. }
  34. Ext.apply(Ext,{
  35. emptyFn : function(){},
  36. applyIf : function(o, c){
  37. if(o && c){
  38. for(var p in c){
  39. if(typeof o[p] == "undefined"){ o[p] = c[p]; }
  40. }
  41. }
  42. return o;
  43. },
  44. extend : function(){
  45. var io = function(o){
  46. for(var m in o){
  47. this[m] = o[m];
  48. }
  49. };
  50. return function(sb, sp, overrides){
  51. if(typeof sp == 'object'){
  52. overrides = sp;
  53. sp = sb;
  54. sb = function(){sp.apply(this, arguments);};
  55. }
  56. var F = function(){}, sbp, spp = sp.prototype;
  57. F.prototype = spp;
  58. sbp = sb.prototype = new F();
  59. sbp.constructor=sb;
  60. sb.superclass=spp;
  61. if(spp.constructor == Object.prototype.constructor){
  62. spp.constructor=sp;
  63. }
  64. sb.override = function(o){
  65. Ext.override(sb, o);
  66. };
  67. sbp.override = io;
  68. Ext.override(sb, overrides);
  69. return sb;
  70. };
  71. }(),
  72. override : function(origclass, overrides){
  73. if(overrides){
  74. var p = origclass.prototype;
  75. for(var method in overrides){
  76. p[method] = overrides[method];
  77. }
  78. }
  79. },
  80. namespace : function(){
  81. var a=arguments, o=null, i, j, d, rt;
  82. for (i=0; i<a.length; ++i) {
  83. d=a[i].split(".");
  84. rt = d[0];
  85. eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
  86. for (j=1; j<d.length; ++j) {
  87. o[d[j]]=o[d[j]] || {};
  88. o=o[d[j]];
  89. }
  90. }
  91. },
  92. urlEncode : function(o){
  93. if(!o){
  94. return "";
  95. }
  96. var buf = [];
  97. for(var key in o){
  98. var ov = o[key], k = encodeURIComponent(key);
  99. var type = typeof ov;
  100. if(type == 'undefined'){
  101. buf.push(k, "=&");
  102. }else if(type != "function" && type != "object"){
  103. buf.push(k, "=", encodeURIComponent(ov), "&");
  104. }else if(ov instanceof Array){
  105. if (ov.length) {
  106. for(var i = 0, len = ov.length; i < len; i++) {
  107. buf.push(k, "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
  108. }
  109. } else {
  110. buf.push(k, "=&");
  111. }
  112. }
  113. }
  114. buf.pop();
  115. return buf.join("");
  116. },
  117. urlDecode : function(string, overwrite){
  118. if(!string || !string.length){
  119. return {};
  120. }
  121. var obj = {};
  122. var pairs = string.split('&');
  123. var pair, name, value;
  124. for(var i = 0, len = pairs.length; i < len; i++){
  125. pair = pairs[i].split('=');
  126. name = decodeURIComponent(pair[0]);
  127. value = decodeURIComponent(pair[1]);
  128. if(overwrite !== true){
  129. if(typeof obj[name] == "undefined"){
  130. obj[name] = value;
  131. }else if(typeof obj[name] == "string"){
  132. obj[name] = [obj[name]];
  133. obj[name].push(value);
  134. }else{
  135. obj[name].push(value);
  136. }
  137. }else{
  138. obj[name] = value;
  139. }
  140. }
  141. return obj;
  142. },
  143. isIE : isIE,
  144. isIE7 : isIE7,
  145. isGecko : isGecko,
  146. isStrict:isStrict
  147. });
  148. })();
  149. Ext.applyIf(String, {
  150. escape : function(string) {
  151. return string.replace(/('|\\)/g, "\\$1");
  152. },
  153. /**
  154. var s = String.leftPad('123', 5, '0');
  155. // s now contains the string: '00123'
  156. */
  157. leftPad : function (val, size, ch) {
  158. var result = new String(val);
  159. if(ch === null || ch === undefined || ch === '') {
  160. ch = " ";
  161. }
  162. while (result.length < size) {
  163. result = ch + result;
  164. }
  165. return result;
  166. },
  167. /**
  168. var cls = 'my-class', text = 'Some text';
  169. var s = String.format('<div class="{0}">{1}</div>', cls, text);
  170. // s now contains the string: '<div class="my-class">Some text</div>'
  171. */
  172. format : function(format){
  173. var args = Array.prototype.slice.call(arguments, 1);
  174. return format.replace(/\{(\d+)\}/g, function(m, i){
  175. return args[i];
  176. });
  177. }
  178. });
  179. Ext.applyIf(Array.prototype, {
  180. indexOf : function(o){
  181. for (var i = 0, len = this.length; i < len; i++){
  182. if(this[i] == o) return i;
  183. }
  184. return -1;
  185. },
  186. remove : function(o){
  187. var index = this.indexOf(o);
  188. if(index != -1){
  189. this.splice(index, 1);
  190. }
  191. }
  192. });

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。