Ext.extend() 体现了程序员非凡的制造轮子的能力。它基于 javascript 古老的对象模型,最大程度地模拟出现代面向对象语言的类型继承的语意。但是在程序界有太多的“与XXX很像”,但是实际上又有很多差别。要想最彻底、最精确地理解 Ext.extend(),最直接(往往也是最有效)的方法就是去读它的源代码。为了达到更好的可读性,我更改了部分变量名称,加上了详细的注释。

  1 /**
  2          * <p>Extends one class to create a subclass and optionally overrides members with the passed literal. This method
  3          * also adds the function "override()" to the subclass that can be used to override members of the class.</p>
  4          * For example, to create a subclass of Ext GridPanel:
  5          * <pre><code>
  6 MyGridPanel = Ext.extend(Ext.grid.GridPanel, {
  7     constructor: function(config) {
  8
  9 //      Create configuration for this Grid.
 10         var store = new Ext.data.Store({...});
 11         var colModel = new Ext.grid.ColumnModel({...});
 12
 13 //      Create a new config object containing our computed properties
 14 //      *plus* whatever was in the config parameter.
 15         config = Ext.apply({
 16             store: store,
 17             colModel: colModel
 18         }, config);
 19
 20         MyGridPanel.superclass.constructor.call(this, config);
 21
 22 //      Your postprocessing here
 23     },
 24
 25     yourMethod: function() {
 26         // etc.
 27     }
 28 });
 29 </code></pre>
 30  *
 31  * <p>This function also supports a 3-argument call in which the subclass's constructor is
 32  * passed as an argument. In this form, the parameters are as follows:</p>
 33  * <div class="mdetail-params"><ul>
 34  * <li><code>subclass</code> : Function <div class="sub-desc">The subclass constructor.</div></li>
 35  * <li><code>superclass</code> : Function <div class="sub-desc">The constructor of class being extended</div></li>
 36  * <li><code>overrides</code> : Object <div class="sub-desc">A literal with members which are copied into the subclass's
 37  * prototype, and are therefore shared among all instances of the new class.</div></li>
 38  * </ul></div>
 39  *
 40  * @param {Function} superclass The constructor of class being extended.
 41  * @param {Object} overrides <p>A literal with members which are copied into the subclass's
 42  * prototype, and are therefore shared between all instances of the new class.</p>
 43  * <p>This may contain a special member named <tt><b>constructor</b></tt>. This is used
 44  * to define the constructor of the new class, and is returned. If this property is
 45  * <i>not</i> specified, a constructor is generated and returned which just calls the
 46  * superclass's constructor passing on its parameters.</p>
 47  * <p><b>It is essential that you call the superclass constructor in any provided constructor. See example code.</b></p>
 48  * @return {Function} The subclass constructor from the <code>overrides</code> parameter, or a generated one if not provided.
 49  */
 50 Ext.extend = function(){
 51     // inline overrides
 52     var io = function(o){
 53         for(var m in o){
 54             this[m] = o[m];
 55         }
 56     };
 57     var oc = Object.prototype.constructor; // 如果一个对象的 constructor == oc,说明它是使用类似 { age:22 } 这种语法创建的字面量对象,
 58                                            // 而且没有对constructor属性赋值
 59
 60     return function(subClass, superClass, overrides){
 61         if(typeof superClass == 'object'){
 62             // 如果 superClass 是对象而不是构造函数,就说明是使用的是
 63             // var Cat = Ext.extend(Animal, { 64             //     say : function() { 65             //         document.writeln("I'm a cat name " + this.name);
 66             //     }
 67             // });
 68             // 这种方式调用的。也就是说返回值是subClass, subClass 参数其实是 superClass,superClass参数其实是overrides,overrides参数应该被忽略
 69             overrides = superClass; // 忽略 overrides 参数
 70             superClass = subClass; // subClass 参数其实是 superClass
 71             // subClass 参数将作为将来的返回值。
 72             // 如果 overrides 对象没有自定义构造函数,为其定义一个,并且里面调用父类构造函数;
 73             // 如果 overrides 对象含有自定义构造函数,把overrides的构造函数赋给子类
 74             subClass = overrides.constructor != oc ? overrides.constructor : function(){
 75                     superClass.apply(this, arguments); // 调用父类构造函数(前提是子类构造函数的参数可以比父类少,但是顺序要一致)
 76                 };
 77         }
 78
 79         // 原型式继承。之所以创建一个临时构造函数F,而不是令 subClass.prototype = new SuperClass,
 80         // 是为了更改子类的prototype的时候不会影响到父类的prototype
 81         var F = function(){},
 82             subPrototype, // 子类构造函数的 Prototype
 83             superPrototype = superClass.prototype; // 父类构造函数的 Prototype
 84
 85         F.prototype = superPrototype;
 86         subPrototype = subClass.prototype = new F();
 87         subPrototype.constructor=subClass;
 88         // 只所以没写成 subClass.superclass=superClass,是为了在overrides对象的constructor方法里
 89         // 可以使用诸如 “MyGridPanel.superclass.constructor.call(this, config)”这种(读起来比较
 90         // 自然的)写法调用父类构造函数。
 91         subClass.superclass=superPrototype;
 92         // 如果 superclass.prototype 是字面量对象,确保 superclass.prototype。constructor 指向 superClass
 93         if(superPrototype.constructor == oc){
 94             superPrototype.constructor=superClass;
 95         }
 96         // 为子类增加一个override()方法。调用 subClass.override(o) 等价于调用 Ext.override(subClass, o)
 97         subClass.override = function(o){
 98             Ext.override(subClass, o);
 99         };
100         // 增加一个名为 superclass() 的实例方法,这样在overrides对象的constructor方法里
101         // 就可以使用诸如 “this.superclass().constructor.call(this, config)”来调用父类
102         // 构造函数,而且没有依赖子类构造函数的名称。
103         subPrototype.superclass = subPrototype.supr = (function(){
104             return superPrototype;
105         });
106         // 为子类增加一个实例方法: override()
107         subPrototype.override = io;
108         // 将 overrides 对象里的方法复写到 subClass.prototype 中
109         Ext.override(subClass, overrides);
110         // 为子类增加一个extend()方法。调用 subClass.extend(o); 等价于调用 Ext.extend(subClass, o);
111         subClass.extend = function(o){return Ext.extend(subClass, o);};
112         return subClass;
113     };
114 }();

下面这张对象图则是执行了 var SubClass = Ext.extend(SuperClass, { someprop : 'some' }) 之后的效果。

[javascript]图解+注释版 Ext.extend()相关推荐

  1. Ext.extend方法

    extend (Object subclass,Object superclass,[Object overrides] : Object 第一个参数:子类 第二个参数:父类 第三个参数:要覆盖的属性 ...

  2. 商品期货策略 之 Python 精简多品种 MACD 趋势策略框架(注释版)

    Python 精简多品种 MACD 趋势策略框架(注释版) Python超级精简的多品种MACD趋势策略框架, 代码超级精简, 注释超级详细啰嗦. >_<! 需要引用 python版CTP ...

  3. Webclient UI view里Javascript的注释问题

    Created by Jerry Wang, last modified on May 23, 2014 在Webclient ui view里使用// 添加Javascript的注释时, runti ...

  4. html5 百度地图api文档,开发指南--百度地图JavaScript API大众版.doc

    开发指南--百度地图JavaScriptAPI大众版开发指南--百度地图JavaScriptAPI大众版 简介 JavaScript API大众版 JavaScript API功能介绍 百度地图Jav ...

  5. html5连连看源码解析,JS连连看源码完美注释版(推荐)

    JS连连看源码完美注释版 table{ border-collapse: collapse; } td{ border: solid #ccc 1px; height: 36px; width: 36 ...

  6. WinAPI入门: 第一个标准Win32窗口程序 [改进详细注释版]

    WinAPI入门: 第一个标准Win32窗口程序 下载链接: 若想立即看到本程序的运行结果,可点击EXE文件的免费下载链接; HelloWin_v1.sfx.exe;–带语音和背景音乐; 相关链接: ...

  7. Ext.extend 与 Ext.define

    转自:http://www.cnblogs.com/CoolHu/archive/2012/11/28/2792803.html SysPanel = function(config) {config ...

  8. 手撕Resnet卷积神经网络-pytorch-详细注释版(可以直接替换自己数据集)-直接放置自己的数据集就能直接跑。跑的代码有问题的可以在评论区指出,看到了会回复。训练代码和预测代码均有。

    Alexnet网络详解代码:手撕Alexnet卷积神经网络-pytorch-详细注释版(可以直接替换自己数据集)-直接放置自己的数据集就能直接跑.跑的代码有问题的可以在评论区指出,看到了会回复.训练代 ...

  9. 使用PyTorch构建GAN生成对抗网络源码(详细步骤讲解+注释版)02 人脸识别 下

    文章目录 1 测试鉴别器 2 建立生成器 3 测试生成器 4 训练生成器 5 使用生成器 6 内存查看 上一节,我们已经建立好了模型所必需的鉴别器类与Dataset类. 使用PyTorch构建GAN生 ...

最新文章

  1. 图像处理_imgproc笔记(1)
  2. springboot 配置双mysql数据库
  3. 查看ubuntu linux开放的端口以及控制端口范围
  4. Crystal Report 2008
  5. 手机python3l运行_Python3 os.lchflags() 方法
  6. pytorch搭建TextRCNN模型与使用案例
  7. SQL Server 2014 新建数据库
  8. php mysql 查询数据库表结构_mysql查询数据库下的表结构?
  9. 第三十 访问财富进退自如 —Spring交易管理
  10. Sql语句查询某列A相同值的另一列B最大值的数据
  11. linux内核编程--4netfiter钩子函数
  12. 想自学单片机,各位有什么书和板子值得推荐的吗?
  13. .NET的.snk文件使用方法
  14. 计算机等级考试四级--数据库原理
  15. 算法分析之大O、大Ω、大Θ和小o表示法
  16. Revit标注墙偏移如何简便标注呢?万能标注?
  17. 【C语言】位段(详解)
  18. 密码学系列 - 国密算法
  19. 我在武汉新东方上过的8个老师
  20. 纪录片中国通史观后感

热门文章

  1. BZOJ3627 [JLOI2014]路径规划
  2. 尝试使用阿里云服务器
  3. AMD推Radeon HD 7790显卡 性价比突出下月开卖
  4. 在Windows Server 2012中配置NAT代理服务器
  5. 免费下载思科CCNP 642-143考试题库
  6. CNKI中银屑病、大肠菌群、内毒素LPS(调研手稿三)
  7. 玻璃体液化研究(控制)
  8. 常见的14种异常心电图的波形特点
  9. Error: Could not find or load main class org.apache.spark.deploy.yarn.ExecutorLauncher
  10. 用scikit-learn进行LDA降维(转载+注释)