Ext.extend方法是用來(lái)實(shí)現(xiàn)類(lèi)的繼承。
extend(Object subclass,Object superclass,[Object overrides] : Object
第一個(gè)參數(shù):子類(lèi)
第二個(gè)參數(shù):父類(lèi)
第三個(gè)參數(shù):要覆蓋的屬性。
這里需要強(qiáng)調(diào)一下,子類(lèi)繼承下來(lái)的是父類(lèi)中通過(guò)superclass.prototype方式定義的屬性(包括用此方法定義的函數(shù))。
例子如下:
- <script type="text/javascript">
- function S(){
- }
- S.prototype.s = "s";
- S.prototype.s1 = "s1";
- function C(){
- this.c = "c";
- this.c1 = "c1";
- }
- Ext.extend(C,S,{s1:"by c overload"});
- var c = new C();
- alert(c.s);
- alert(c.s1);
- </script>
- <script type="text/javascript">
- function S(){
- }
- S.prototype.s = "s";
- S.prototype.s1 = "s1";
- function C(){
- this.c = "c";
- this.c1 = "c1";
- }
- Ext.extend(C,S,{s1:"by c overload"});
- var c = new C();
- alert(c.s);
- alert(c.s1);
- </script>
<script type="text/javascript">
function S(){
}
S.prototype.s = "s";
S.prototype.s1 = "s1";
function C(){
this.c = "c";
this.c1 = "c1";
}
Ext.extend(C,S,{s1:"by c overload"});
var c = new C();
alert(c.s); //s
alert(c.s1); //by c overload
</script>
如果按下面這個(gè)方式寫(xiě)就會(huì)提示c.s沒(méi)有定義(undefind):
- <script type="text/javascript">
- function S(){
- this.s = "s";
- this.s1 = "s1";
- }
- function C(){
- this.c = "c";
- this.c1 = "c1";
- }
- Ext.extend(C,S,{s1:"by c overload"});
- var c = new C();
- alert(c.s);
- alert(c.s1);
- </script>
- <script type="text/javascript">
- function S(){
- this.s = "s";
- this.s1 = "s1";
- }
- function C(){
- this.c = "c";
- this.c1 = "c1";
- }
- Ext.extend(C,S,{s1:"by c overload"});
- var c = new C();
- alert(c.s);
- alert(c.s1);
- </script>
<script type="text/javascript">
function S(){
this.s = "s";
this.s1 = "s1";
}
function C(){
this.c = "c";
this.c1 = "c1";
}
Ext.extend(C,S,{s1:"by c overload"});
var c = new C();
alert(c.s); //undefind
alert(c.s1); //by c overload
</script>
也可以通過(guò)如下方式來(lái)實(shí)現(xiàn)類(lèi)的繼承
- <script type="text/javascript">
- function S(){
- }
- S.prototype.s = "s";
- S.prototype.s1 = "s1";
- C = Ext.extend(S,{s1:"by c overload"});
- var c = new C();
- alert(c.s);
- alert(c.s1);
- </script>
- <script type="text/javascript">
- function S(){
- }
- S.prototype.s = "s";
- S.prototype.s1 = "s1";
- C = Ext.extend(S,{s1:"by c overload"});
- var c = new C();
- alert(c.s);
- alert(c.s1);
- </script>