<script type="text/javascript">
Ext.onReady(function(){
function config() {
this.id = 5;
this.name = 'myclass';
this.age = 15;
};
A = function (config) {
this.id = config.id;
}
A.prototype.sayHello = function(){
alert("function from class A!");
}
/*
子类有自己定义的构造函数
超类A的属性(id)没有被B继承过来。
因为Ext.extend()函数里面的if(typeof sp == 'object'){…}不成立,在继承之后,子类B仅仅获得了超类的sayHello()方法。
*/
B = function(config){
this.name = config.name;
}
B = Ext.extend(B,A);
// new B(new config()).sayHello();
// alert(new B(new config()).id);
/*
不提供子类自己的构造函数
这样的话子类C可以顺利地获得超类A的id属性和sayHello()方法
*/
C = Ext.extend(A,{
id:77,
who:'c Class'
});
// alert(new C(new config()).id);
// alert(new C(new config()).who);
// new C(new config()).sayHello();
/*
在overrides里面提供constructor
必须加一句调用:this.superclass.constructor.call,这句实际上是调用父类的构造器来使子类获得父类里面定义的参数和方法。
但是这么做有一点不是很爽,例如我要自定义一个MyPanel来继承Ext.Panel,如果我想采用第三种写法,自己提供的overrides里面必须有如上的手工调用的语句。如果继承的层次稍微深一点,那么每次继承都要写同一句话,这显然是很不爽快的事情。
*/
D = function(config){
this.age = config.age;
D.superclass.constructor.call(this,config);
}
Ext.extend(D,A,{});
alert(new D(new config()).id);
alert(new D(new config()).age);
new D(new config()).sayHello();
var d = new D({
age:45,
id:88
});
alert(d.id);
alert(d.age);
d.sayHello();
});
</script>