Ext.extend方法是用來實現類的繼承。
extend(Object subclass,Object superclass,[Object overrides] : Object
第一個參數:子類
第二個參數:父類
第三個參數:要覆蓋的屬性。
這里需要強調一下,子類繼承下來的是父類中通過superclass.prototype方式定義的屬性(包括用此方法定義的函數)。
例子如下:
- <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>
如果按下面這個方式寫就會提示c.s沒有定義(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>
也可以通過如下方式來實現類的繼承
- <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>