版本Ext2.02提供了一个extend函数实现继承,该函数会将父类的prototype里的所有字段和方法复制到子类的prototype里,重定义的字段或方法会override父类的同名方法。今天要扩展HtmlEditor类,但不知怎样才能调用父类的方法,由于刚接触Ext2,研究了很久才想到下面的方法。比如:

Ext.A = function() ......{
this.s1 = " S1";
}
Ext.A.prototype = ......{
s2 : " msg",
show : function(str) ......{
alert(str);
},
doShow : function() ......{
this.show('AAAAAAAA');
}
}
//继承Ext.A
Ext.B = Ext.extend(Ext.A, ...{
vk : " viking",
show : function(str) ...{
alert('BBBBBBBBB ' + str +this.s2 + this.s1);
}
});
var b = new Ext.B();
b.doShow();b中不能访问类A中的show,如果要执行A中show有两种方法:
- 把A.show的代码复制到B.show中。最笨,这样的话还要继承干嘛!
- 使用函数createSequence。该函数Ext内建Function类的方法,用法看实例。

Ext.A = function() ...{
this.s1 = " S1";
}
Ext.A.prototype = ...{
s2 : " msg",
show : function(str) ...{
alert(str);
},
doShow : function() ...{
this.show('AAAAAAAA');
}
}

Ext.B = Ext.extend(Ext.A, ...{
vk : " viking",
show : Ext.A.prototype.show.createSequence(function(str) ...{
alert('BBBBBBBBB ' + str +this.s2);
alert(this.s1);
//Ext.B.supperclass.show();
})
});

Ext.C = Ext.extend(Ext.B, ...{
show : Ext.B.prototype.show.createSequence(function(str) ...{
alert('CCCCCCCCC ' + str + this.vk + this.s2);
})
})
var c = new Ext.C();
c.doShow();执行c.doShow()会调用A.show()、B.show()、C.show()。如此就可以避免复制父类的代码,实现对父类方法的功能扩展。如果有其他更好的方法,麻烦告诉我!
136

被折叠的 条评论
为什么被折叠?



