【问题标题】:how to call the superclass in ext js 3.4 within a variable如何在变量中调用 ext js 3.4 中的超类
【发布时间】:2026-01-12 07:40:01
【问题描述】:

有谁知道如何在 ext js 3.4 中成功调用超类? 在我的示例代码中,我似乎无法让超类在面板上工作。

MyWindow = Ext.extend(MyWindowUi, {
    initComponent: function() {
        MyWindow.superclass.initComponent.call(this);
        this.createTextField();
    },  
    createTextField : function() {  
        var p = new Ext.Panel({
            title: 'somepanel',
            initComponent: function() {
                console.log('init component');
                console.log(this);
                this.superclass.initComponent.call(this); //NOT WORKING??
            }
        });
        this.add(p);
    }
});

【问题讨论】:

    标签: extjs extjs3


    【解决方案1】:

    改用Ext.Panel.superclass.initComponent.call(this)

    【讨论】:

    • 我尝试使用超类,但没有呈现面板。 Ext.Panel.prototype.initComponent.call(this);为我工作。
    【解决方案2】:

    它不起作用,因为您在创建 p 时没有调用 Ext.Panel 的 initComponent。如果你打算用前一个方法的超类调用来覆盖一个方法,如果不需要范围技巧,Ext.extend 会更干净。

    你也可以这样做:

       createTextField : function() {  
        var p = new Ext.Panel({
            title: 'somepanel'
        }),
        oldInit = p.initComponent;
    
        p.initComponent = function() {
            console.log('init component');
            console.log(this);
            oldInit.call(this);
        }
    
        this.add(p);
    }
    

    或者类似的东西:

    createTextField : function() {  
        var p = new Ext.Panel({
            title: 'somepanel',
            initComponent: function() {
                console.log('init component');
                console.log(this);
                Ext.Panel.prototype.initComponent.call(this);
            }
        });
        this.add(p);
    }
    

    但他们都是丑陋的imo。

    【讨论】:

    • 谢谢!超级,选项:Ext.Panel.prototype.initComponent.call(this);效果很好!