ExtJS2的继承

版本Ext2.02提供了一个extend函数实现继承,该函数会将父类的prototype里的所有字段和方法复制到子类的prototype里,重定义的字段或方法会override父类的同名方法。今天要扩展HtmlEditor类,但不知怎样才能调用父类的方法,由于刚接触Ext2,研究了很久才想到下面的方法。比如:

 

Ext.A  =   function () ... {
    
this.s1 = " S1";
}

Ext.A.prototype 
=  ... {
    s2 : 
" msg",
    show : 
function(str) ...{
        alert(str);
    }
,
    doShow : 
function() ...{
        
this.show('AAAAAAAA');
    }

}


// 继承Ext.A
Ext.B  =  Ext.extend(Ext.A,  {
    vk : 
" viking",
    show : 
function(str) {
        alert(
'BBBBBBBBB ' + str +this.s2 + this.s1);
    }

}
);

var  b  =   new  Ext.B();
b.doShow();

b中不能访问类A中的show,如果要执行A中show有两种方法:

  1. 把A.show的代码复制到B.show中。最笨,这样的话还要继承干嘛!
  2. 使用函数createSequence。该函数Ext内建Function类的方法,用法看实例。

 

Ext.A  =   function ()  {
        
this.s1 = " S1";
}

Ext.A.prototype 
=   {
        s2 : 
" msg",
    show : 
function(str) {
            alert(str);
        }
,
    doShow : 
function() {
           
this.show('AAAAAAAA');
        }

}


Ext.B 
=  Ext.extend(Ext.A,  {
        vk : 
" viking",
    show : Ext.A.prototype.show.createSequence(
function(str) {
            alert(
'BBBBBBBBB ' + str +this.s2);
            alert(
this.s1);
        
//Ext.B.supperclass.show();
        }
)
}
);

Ext.C 
=  Ext.extend(Ext.B,  {
    show : Ext.B.prototype.show.createSequence(
function(str) {
            alert(
'CCCCCCCCC ' + str + this.vk + this.s2);
    }
)
}
)
var  c  =   new  Ext.C();
c.doShow();

执行c.doShow()会调用A.show()、B.show()、C.show()。如此就可以避免复制父类的代码,实现对父类方法的功能扩展。如果有其他更好的方法,麻烦告诉我!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值