继承
Ext JS也拥有“面向对象”的概念:封装、继承和多态等。当定义一个新的类的的时候,可以使用extend这个关键词来从已知的类继承。
先定义一个父类,其中有个getName的方法:
Ext.define('Person',
{
name : 'Unknown',
constructor : function(name){
if(name){
this.name = name;
}
},
getName : function(){
alert("My name is " + this.name);
}
});
紧接着,定义一个子类:
Ext.define('Student',
{
extend : 'Person',
schoolName : 'Unknown',
constructor : function(name, schoolName){
this.schoolName = schoolName || 'Unknown';
//call parent class constructor
this.callParent(arguments);
},
getSchoolName : function(){
alert("My school name is " + this.schoolName);
}
});
var newStudent = new Student('XYZ', 'ABC School');
newStudent.getName(); //output: XYZ
newStudent.getSchoolName(); //output: ABC School
像上面这样,Student类使用extend关键词来继承Person类,当我们在尝试调用子类的getName()的方法时,实际上调用的是父类的方法。