ExtJS之面向?qū)ο缶幊?br />
1:支持命名空間
// 定義一個命名空間
Ext.namespace("Ext.wentao");
// 在命名空間上定義一個類
Ext.wentao.helloworld = Ext.emptyFn;
// 創(chuàng)建一個類的實(shí)例
new Ext.wentao.helloworld();
</script>
其中
等價(jià)于
2:支持類實(shí)例屬性
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Ext.wentao.Person = Ext.emptyFn; // 在命名空間上自定義一個類
// 為自定義的類 增加一個 name 屬性,并賦值
Ext.apply(Ext.wentao.Person.prototype, {
name : "劉文濤"
});
var _person = new Ext.wentao.Person();// 實(shí)例化 自定義類
alert(_person.name);
_person.name = "張三";// 修改類name屬性
alert(_person.name);
3:支持類實(shí)例方法
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Ext.wentao.Person = Ext.emptyFn; // 在命名空間上自定義一個類
// 演示類實(shí)例方法
Ext.apply(Ext.wentao.Person.prototype, {
name : "劉文濤",
sex : "男",
print : function() {
alert(String.format("姓名:{0},性別:{1}", this.name, this.sex));
}
});
var _person = new Ext.wentao.Person();// 實(shí)例化 自定義類
_person.print();
4:支持類靜態(tài)方法

Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Ext.wentao.Person = Ext.emptyFn; // 在命名空間上自定義一個類
// 演示類實(shí)例方法
Ext.apply(Ext.wentao.Person.prototype, {
name : "劉文濤",
sex : "男",
print : function() {
alert(String.format("姓名:{0},性別:{1}", this.name, this.sex));
}
});
// 演示 類靜態(tài)方法
Ext.wentao.Person.print = function(_name, _sex) {
var _person = new Ext.wentao.Person();
_person.name = _name;
_person.sex = _sex;
_person.print(); // 此處調(diào)用類 實(shí)例方法,上面print是類 靜態(tài)方法
};
Ext.wentao.Person.print("張三", "女"); // 調(diào)用類 靜態(tài)方法
</script>
5:支持構(gòu)造方法

Ext.namespace("Ext.wentao"); //自定義一個命名空間


Ext.wentao.Person = function(_cfg){
Ext.apply(this,_cfg);
};


Ext.apply(Ext.wentao.Person.prototype, {
print:function(){
alert(String.format("姓名:{0},性別:{1}",this.name,this.sex));
}
});


Ext.wentao.Person.print = function(_name,_sex){
var _person = new Ext.wentao.Person({name:_name,sex:_sex});
_person.print(); //此處調(diào)用類 實(shí)例方法,上面print是類 靜態(tài)方法
};


</script>
6:支持類繼承

Ext.namespace("Ext.wentao"); // 自定義一個命名空間
// *******************父類*********************
// 構(gòu)造方法
Ext.wentao.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
// 演示類實(shí)例方法
Ext.apply(Ext.wentao.Person.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Ext.wentao.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
Ext.extend(Ext.wentao.Student, Ext.wentao.Person, {
job : "學(xué)生"
});
var _student = new Ext.wentao.Student({
name : "張三",
sex : "女"
});
_student.print(); // 調(diào)用 父類方法

7:支持類實(shí)例方法重寫

Ext.namespace("Ext.wentao"); // 自定義一個命名空間
// *******************父類*********************
// 構(gòu)造方法
Ext.wentao.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
// 演示類實(shí)例方法
Ext.apply(Ext.wentao.Person.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Ext.wentao.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
// 重寫父類的 實(shí)例 方法
Ext.extend(Ext.wentao.Student, Ext.wentao.Person, {
job : "學(xué)生",
print : function() {
alert(String.format("{0}是一位{1}{2}", this.name, this.sex,
this.job));
}
});
var _student = new Ext.wentao.Student({
name : "張三",
sex : "女"
});
_student.print(); // 調(diào)用 父類方法
8:支持命名空間別名
Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Wt = Ext.wentao; // 命名空間的別名
// *******************父類*********************
// 構(gòu)造方法
Wt.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
// 演示類實(shí)例方法
Ext.apply(Wt.Person.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Wt.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
// 重寫父類的 實(shí)例 方法
Ext.extend(Wt.Student, Ext.wentao.Person, {
job : "學(xué)生",
print : function() {
alert(String.format("{0}是一位{1}{2}", this.name, this.sex,
this.job));
}
});
var _student = new Wt.Student({
name : "張q三",
sex : "女"
});
_student.print(); // 調(diào)用 父類方法
</script>
9:支持類別名

Ext.namespace("Ext.wentao"); // 自定義一個命名空間
Wt = Ext.wentao; // 命名空間的別名
// *******************父類*********************
// 構(gòu)造方法
Wt.Person = function(_cfg) {
Ext.apply(this, _cfg);
};
PN = Wt.Person; // 類別名
// 演示類實(shí)例方法
Ext.apply(PN.prototype, {
job : "無",
print : function() {
alert(String.format("姓名:{0},性別:{1},角色:{2}", this.name,
this.sex, this.job));
}
});
// *******************子類1*********************
Wt.Student = function(_cfg) {
Ext.apply(this, _cfg);
};
ST = Wt.Student;
// 重寫父類的 實(shí)例 方法
Ext.extend(ST, PN, {
job : "學(xué)生",
print : function() {
alert(String.format("{0}是一位{1}{2}", this.name, this.sex,
this.job));
}
});
var _student = new ST({
name : "張q三",
sex : "女"
});
_student.print(); // 調(diào)用 父類方法