Better way to call superclass method in ExtJS

后端 未结 6 1474
梦如初夏
梦如初夏 2020-12-24 06:30

All the ExtJS documentation and examples I have read suggest calling superclass methods like this:

MyApp.MyPanel = Ext.extend(Ext.Panel, {
  initComponent: f         


        
6条回答
  •  盖世英雄少女心
    2020-12-24 07:09

    I've came up with this solution a couple hours ago heheh...

    function extend (parentObj, childObj) {
        parentObj = parentObj || function () {};
    
        var newObj = function () {
            if (typeof this.initialize == 'function') {
                this.initialize.apply(this, arguments);
            }
        }
    
        newObj.prototype.__proto__ = parentObj.prototype;
    
        for (var property in childObj) {
            newObj.prototype[property] = childObj[property];
        }
    
        newObj.prototype.superclass = function (method) { 
            var callerMethod = arguments.callee.caller,
                currentProto = this.constructor.prototype.__proto__;
    
            while (callerMethod == currentProto[method]) {
                currentProto = currentProto.__proto__;
            } 
    
            return currentProto[method]; 
        };
    
        return newObj;
    }
    

    Then you can do:

    var A = function () { 
        this.name = "A Function!";
    };
    
    A.prototype.initialize = function () {
        alert(this.name);
    }
    
    var B = extend(A, {
        initialize: function () {
            this.name = "B Function!";
            this.superclass('initialize').apply(this);
        }
    });
    
    var C = extend(B, {
        initialize: function () {
            this.superclass('initialize').apply(this);
        }
    });
    

    Tested only with (Chromium 8.0.552.237 (70801) Ubuntu 10.10) and (Firefox 3.6.13).

    Hope this help someone, I was almost switching to GWT.

提交回复
热议问题