---
title: "ExtJS精华函数解析(仅个人见解)"
author: "sailorlee"
date: "2008-12-29T11:44:08Z"
tags: ["C", "C#", "C++", "EXT", "prototype"]
slug: "extjs"
draft: false
lastmod: "2008-12-29T11:44:08Z"
id: 306226
url: "https://sailorlee.iteye.com/blog/306226"
---

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/forum/showthread.php?t=13213)    [http://extjs.com/learn/Image:Face.jpg.jpg](http://extjs.com/learn/Image:Face.jpg.jpg)  
        Ext.extend(sb, sp, overrides)即sb扩展sp的所有对象,overriders即你想增加的对象或者方法.可以覆盖sp的对象也与方法及属性.

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

001. /*
002. \*
003. \* Author:Sloppy
004. \* Date:2007-09-15
005. \* Contact:QQ:35734786
006. \*/
007. if(typeof(Ext)=="undefined"){
008. var Ext={};
009. }
010. window\["undefined"] = window\["undefined"];
011. Ext.apply = function(o, c, defaults){
012. if(defaults){
013. Ext.apply(o, defaults);
014. }
015. if(o && c && typeof c == 'object'){
016. for(var p in c){
017. o\[p] = c\[p];
018. }
019. }
020. return o;
021. };
022. (function(){
023. var ua = navigator.userAgent.toLowerCase();
024. var isStrict = document.compatMode == "CSS1Compat",
025. isSafari = (/webkit|khtml/).test(ua),
026. isIE = ua.indexOf("msie") &gt; -1,
027. isIE7 = ua.indexOf("msie 7") &gt; -1,
028. isGecko = !isSafari && ua.indexOf("gecko") &gt; -1;
029. if(isIE && !isIE7){
030. try{
031. document.execCommand("BackgroundImageCache", false, true);
032. }catch(e){}
033. }
034. Ext.apply(Ext,{
035. emptyFn : function(){},
036. applyIf : function(o, c){
037. if(o && c){
038. for(var p in c){
039. if(typeof o\[p] == "undefined"){ o\[p] = c\[p]; }
040. }
041. }
042. return o;
043. },
044. extend : function(){
045. var io = function(o){
046. for(var m in o){
047. this\[m] = o\[m];
048. }
049. };
050. return function(sb, sp, overrides){
051. if(typeof sp == 'object'){
052. overrides = sp;
053. sp = sb;
054. sb = function(){sp.apply(this, arguments);};
055. }
056. var F = function(){}, sbp, spp = sp.prototype;
057. F.prototype = spp;
058. sbp = sb.prototype = new F();
059. sbp.constructor=sb;
060. sb.superclass=spp;
061. if(spp.constructor == Object.prototype.constructor){
062. spp.constructor=sp;
063. }
064. sb.override = function(o){
065. Ext.override(sb, o);
066. };
067. sbp.override = io;
068. Ext.override(sb, overrides);
069. return sb;
070. };
071. }(),
072. override : function(origclass, overrides){
073. if(overrides){
074. var p = origclass.prototype;
075. for(var method in overrides){
076. p\[method] = overrides\[method];
077. }
078. }
079. },
080. namespace : function(){
081. var a=arguments, o=null, i, j, d, rt;
082. for (i=0; i&lt;a.length; ++i) {
083. d=a\[i].split(".");
084. rt = d\[0];
085. eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
086. for (j=1; j&lt;d.length; ++j) {
087. o\[d\[j]]=o\[d\[j]] || {};
088. o=o\[d\[j]];
089. }
090. }
091. },
092. urlEncode : function(o){
093. if(!o){
094. return "";
095. }
096. var buf = \[];
097. for(var key in o){
098. var ov = o\[key], k = encodeURIComponent(key);
099. 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 &lt; 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 &lt; 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 &lt; size) {
163. result = ch + result;
164. }
165. return result;
166. },
167. /\**
168. var cls = 'my-class', text = 'Some text';
169. var s = String.format('&lt;div class="{0}"&gt;{1}&lt;/div&gt;', cls, text);
170. // s now contains the string: '&lt;div class="my-class"&gt;Some text&lt;/div&gt;'
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 &lt; 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. });