ExtJs源码分析与学习—ExtJs核心代码(一)

首先声明一下,以下部分内容摘自网上

 

一、 有了上一篇ExtJs源码结构和ExtJs自带的调试工具后,下面介绍一下ExtJs最核心的部分ext-core
二、 首先看Ext.js文件

Js代码    收藏代码
  1. window.undefined = window.undefined;  

 

该段代码是为了兼容不支持window.undefined对象的老版浏览器(估计现在没人用老版浏览器了,呵呵),详细说明可参考

有关window.undefined=window.undefined写法的理解

 

Js代码    收藏代码
  1. Ext = {  
  2.     /** 
  3.      * The version of the framework 
  4.      * @type String 
  5.      */  
  6.     version : '3.3.1',  
  7.     versionDetail : {  
  8.         major : 3,  
  9.         minor : 3,  
  10.         patch : 1  
  11.     }  
  12. };  

 

以上代码定义全局变量Ext

 

Js代码    收藏代码
  1. /** 
  2.  * ExtJs实现对象继承 
  3.  * Copies all the properties of config to obj. 
  4.  * @param {Object} obj The receiver of the properties 
  5.  * @param {Object} config The source of the properties 
  6.  * @param {Object} defaults A different object that will also be applied for default values 
  7.  * @return {Object} returns obj 
  8.  * @member Ext apply 
  9.  */  
  10. Ext.apply = function(o, c, defaults){  
  11.     // no "this" reference for friendly out of scope calls  
  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. };  

 

ExtJs核心代码之一,主要实现对象的继承
该方法有两种执行
其一,只传o,c时直接将c上的所有属性/方法拷贝给o后返回;
其二,defaults也传时,会将defaults,c上的所有属性/方法都拷贝给o ,传三个参数时,首先拷贝defaults,然后再拷贝c,这个方法的实现蛮有意思的。
有了该方法后,ExtJs中对象之间的继承就变得容易多了,只要调用该方法,就可以把A对象上的所有方法复制到B对象上。

 

Js代码    收藏代码
  1. /** 
  2.  * Copies all the properties of config to obj if they don't already exist. 
  3.  * @param {Object} obj The receiver of the properties 
  4.  * @param {Object} config The source of the properties 
  5.  * @return {Object} returns obj 
  6.  */  
  7. applyIf : function(o, c){  
  8.     if(o){  
  9.         for(var p in c){  
  10.             if(!Ext.isDefined(o[p])){  
  11.                 o[p] = c[p];  
  12.             }  
  13.         }  
  14.     }  
  15.     return o;  
  16. },  

 

Ext.apply方法进行复制,它会覆盖子对象中的同名属性或方法的,ExtJs为了避免这种不必要的覆盖有提供了Ext.applyIf函数,该函数可以解决这个问题,在进行属性和方法复制时会判断子对象中是否已存在。
接下来是一个匿名函数,而且该匿名函数在初始化时即运行。执行完后给Ext上扩展了许多实用的属性和方法。
在匿名函数中首先定义了一些局部变量,比如判断浏览器种类、操作系统等。接下来的代码解决了IE6下css背景图不缓存的bug

 

Js代码    收藏代码
  1. // remove css image flicker  
  2.     // 解决IE6下css背景图不缓存bug  
  3.     if(isIE6){  
  4.         try{  
  5.             DOC.execCommand("BackgroundImageCache"falsetrue);  
  6.         }catch(e){}  
  7.     }  

 

实现原理及说明可参见

解决IE6背景图片不缓存的BUG

 

接下来就是Ext.apply()的应用了,扩展了许多Ext实用方法和属性

 

其中Ext.isStrict 并非判断html文档模式为严格模式,而是指标准模式,如<!DOCTYPE HTML>声明会返回true。详细说明可参考用doctype激活浏览器模式 ,国内的秦歌 翻译了该篇文章

 

Js代码    收藏代码
  1. /** 
  2. dom节点id,没有设置ExtJs会默认生成 
  3.  * Generates unique ids. If the element already has an id, it is unchanged 
  4.  * @param {Mixed} el (optional) The element to generate an id for 
  5.  * @param {String} prefix (optional) Id prefix (defaults "ext-gen") 
  6.  * @return {String} The generated Id. 
  7.  */  
  8. id : function(el, prefix){  
  9.     el = Ext.getDom(el, true) || {};  
  10.     if (!el.id) {  
  11.         el.id = (prefix || "ext-gen") + (++idSeed);  
  12.     }  
  13.     return el.id;  
  14. },  

 

该方法为dom元素生成唯一id,如果该元素id存在则不重新生成。默认id前缀为ext-gen,也可通过传参prefix改变

 

ECMAScript 5已经发布半年多了,添加了一些新的API方法,如Array的indexOf,forEach等方法,部分新版本浏览器已经支持这些方法来,但我们想为老的浏览器扩展该方法。可能会这样写

 

Js代码    收藏代码
  1. var proto = Array.prototype; if(!proto.indexOf){ proto.indexOf = function(){ // ... } } if(!proto.forEach){ proto.forEach = function(){ // ... } }   

 

上面书写即保证优先使用浏览器原生支持的API方法,不支持的使用自定义实现的。但这里每次都需要判断下Array原型上是否存在该方法。
google closure   实现方式类似使用了三元运算符,每次都要判断下,相对丑陋。网上有一些对google closure的 批评 ,及一些效率低下的 具体分析   ,批评者甚至包括大牛:Dmitry Baranovskiy 。相对来说,Ext.applyif则使的API的扩展很优雅。

这里插入些题外话,关于提高js运行效率,老外写的

http://blogs.sitepoint.com/2009/11/12/google-closure-how-not-to-write-javascript/

From array.js, line 63:

 

Js代码    收藏代码
  1. for (var i = fromIndex; i < arr.length; i++) {  

 

This for loop looks up the .length property of the array (arr) each time through the loop. Simply by setting a variable to store this number at the start of the loop, you can make the loop run much faster:

 

Js代码    收藏代码
  1. for (var i = fromIndex, ii = arr.length; i < ii; i++) {   

 

Google’s developers seem to have figured this trick out later on in the same file. From array.js, line 153:

 

Js代码    收藏代码
  1. var l = arr.length; // must be fixed during loop... see docs ⋮ for (var i = l - 1; i >= 0; --i) {   

 

 

This loop is better in that it avoids a property lookup each time through the loop, but this particular for loop is so simple that it could be further simplified into a while loop, which will run much faster again:

Js代码    收藏代码
  1. var i = arr.length; ⋮ while (i--) {   

 

 

 

 

 

接下来是Ext.extend方法,该方法也是核心方法之一,整个ext框架继承都是以该方法来扩展的。该方法实现依赖于Ext.override,先看override

 

Js代码    收藏代码
  1. /** 
  2.          * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name. 
  3.          * Usage:<pre><code> 
  4. Ext.override(MyClass, { 
  5.     newMethod1: function(){ 
  6.         // etc. 
  7.     }, 
  8.     newMethod2: function(foo){ 
  9.         // etc. 
  10.     } 
  11. }); 
  12. </code></pre> 
  13.          * @param {Object} origclass The class to override 
  14.          * @param {Object} overrides The list of functions to add to origClass.  This should be specified as an object literal 
  15.          * containing one or more methods. 
  16.          * 将对象overrides的所有属性/方法拷贝到类origclass的原型上。 
  17.          * 需要注意的是后面的if判断,IE中for in不能遍历对象的Object的toSting等方法, 
  18.          * 因此需要特别处理一下。IE9 beta重写对象的内置方法如toString后是可用for in遍历的 
  19.          * @method override 
  20.          */  
  21.         override : function(origclass, overrides){  
  22.             if(overrides){  
  23.                 var p = origclass.prototype;  
  24.                 Ext.apply(p, overrides);  
  25.                 if(Ext.isIE && overrides.hasOwnProperty('toString')){  
  26.                     p.toString = overrides.toString;  
  27.                 }  
  28.             }  
  29.         },  

 

该方法实现了将对象overrides的所有属性/方法拷贝到类origclass的原型上。需要注意,IE中for in不能遍历对象的Object的toSting等方法,因此需要特别处理一下。在IE9 beta重写对象的内置方法如toString后是可用for in遍历的,见 for in的缺陷  。

 

Ext.extend是js继承最经典的实现方式了,参考我之前写的 ExtJs中继承的实现与理解—extend

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于eclipse、s2sh和extjs源码的下载,我们可以采取以下步骤: 1. 首先,我们需要使用搜索引擎,如Google或百度,在相关的官方网站或开源社区中搜索Eclipse、S2SH和ExtJS源码下载链接。 2. 对于Eclipse的源码,我们可以访问Eclipse官方网站(https://www.eclipse.org/downloads/)下载Eclipse IDE的安装包,然后在安装包中找到源码文件。 3. 关于S2SH(Struts2+Spring+Hibernate)的源码,我们可以在Struts2、Spring和Hibernate的官方网站上找到相应的源码下载链接。我们可以访问它们的官方网站,如Struts2(https://struts.apache.org/download.cgi)、Spring(https://spring.io/projects/spring-framework)和Hibernate(https://hibernate.org/orm/downloads/)。 4. 对于ExtJS源码,我们可以访问Sencha公司(原ExtJS开发公司)的官方网站(https://www.sencha.com/products/extjs/#overview)下载相应的版本。在官方网站上,我们可以找到下载源码的链接或通过付费订阅来获取它们。 需要注意的是,这些框架的源码通常是以压缩包的形式提供的,我们需要下载后解压缩才能查看源码。此外,对于如此流行的框架,也可以在许多开源社区中找到相应的源码下载链接,如GitHub、CodePlex、Bitbucket等。在这些社区搜索相应的框架名称,通常可以找到相应的源码仓库。 总的来说,下载Eclipse、S2SH和ExtJS源码需要访问官方网站或开源社区,并根据相关的下载链接或搜索结果来获取源码文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值