Chinaunix首页 | 论坛 | 博客
  • 博客访问: 666337
  • 博文数量: 220
  • 博客积分: 10487
  • 博客等级: 上将
  • 技术积分: 2072
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-09 00:25
文章分类

全部博文(220)

文章存档

2012年(5)

2011年(38)

2010年(135)

2009年(42)

我的朋友

分类:

2010-03-16 22:32:32

相信很多人都了解或听过RIA流行界面框架ExtJs,以其优雅的代码风格和清爽绚丽的界面表现在众多界面框架中脱颖而出,受到国内众多程序员的青睐。它甚至颠覆传统页面设计的习惯,借鉴java Swing设计思想,可以用纯JS来描绘页面。

最近阅读Ext源码时,偶然发现一段异常难读的代码。特在此贴出,借此机会进一步了解javascript.

以下是ext-base-debug.js其中的某段代码:

/**
         *

Extends one class to create a subclass and optionally overrides members with the passed literal. This method
         * also adds the function "override()" to the subclass that can be used to override members of the class.


         * For example, to create a subclass of Ext GridPanel:
         *

MyGridPanel = Ext.extend(Ext.grid.GridPanel, {
    constructor: function(config) {

// Create configuration for this Grid.
        var store = new Ext.data.Store({...});
        var colModel = new Ext.grid.ColumnModel({...});

// Create a new config object containing our computed properties
// *plus* whatever was in the config parameter.
        config = Ext.apply({
            store: store,
            colModel: colModel
        }, config);

        MyGridPanel.superclass.constructor.call(this, config);

// Your postprocessing here
    },

    yourMethod: function() {
        // etc.
    }
});

         *
         *

This function also supports a 3-argument call in which the subclass's constructor is
         * passed as an argument. In this form, the parameters are as follows:


         *

             *
  • subclass : Function
    The subclass constructor.

  •          *
  • superclass : Function
    The constructor of class being extended

  •          *
  • overrides : Object
    A literal with members which are copied into the subclass's
             * prototype, and are therefore shared among all instances of the new class.

  •          *

         *
         * @param {Function} superclass The constructor of class being extended.
         * @param {Object} overrides

A literal with members which are copied into the subclass's
         * prototype, and are therefore shared between all instances of the new class.


         *

This may contain a special member named constructor. This is used
         * to define the constructor of the new class, and is returned. If this property is
         * not specified, a constructor is generated and returned which just calls the
         * superclass's constructor passing on its parameters.


         *

It is essential that you call the superclass constructor in any provided constructor. See example code.


         * @return {Function} The subclass constructor from the overrides parameter, or a generated one if not provided.
         */
        extend : function(){
            // inline overrides

            var io = function(o){
                for(var m in o){
                    this[m] = o[m];
                }
            };
            var oc = Object.prototype.constructor;

            return function(sb, sp, overrides){
                if(Ext.isObject(sp)){
                    overrides = sp;
                    sp = sb;
                    sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};
                }
                var F = function(){},
                    sbp,
                    spp = sp.prototype;

                F.prototype = spp;
                sbp = sb.prototype = new F();
                sbp.constructor=sb;
                sb.superclass=spp;
                if(spp.constructor == oc){
                    spp.constructor=sp;
                }
                sb.override = function(o){
                    Ext.override(sb, o);
                };
                sbp.superclass = sbp.supr = (function(){
                    return spp;
                });
                sbp.override = io;
                Ext.override(sb, overrides);
                sb.extend = function(o){return Ext.extend(sb, o);};
                return sb;
            };
        }(),


阅读(890) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~