|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-12-13 关键字: ext.data
EXT.data下各个类之间的关系与具体作用是什么?
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-13
都没有人研究这方面的吗?
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-14
你不妨先自己分析一下源码,然后看看具体哪个类的作用不明白,再问
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-14
在Ext.util.MixedCollection中,Ext.util.MixedCollection.superclass.constructor.call(this);是什么意思?
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-14
调用超类的构造函数
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-14
还有,在Ext中,类一般都是这样写的
Ext.util.MixedCollection = function(allowFunctions, keyFn){
this.items = [];
this.map = {};
this.keys = [];
this.length = 0;
this.addEvents({
"clear" : true,
。。。。。。
});
this.allowFunctions = allowFunctions === true;
if(keyFn){
this.getKey = keyFn;
}
Ext.util.MixedCollection.superclass.constructor.call(this);
};
Ext.extend(Ext.util.MixedCollection, Ext.util.Observable, {
allowFunctions : false,
add : function(key, o){
if(arguments.length == 1){
o = arguments[0];
key = this.getKey(o);
}
if(typeof key == "undefined" || key === null){
this.length++;
this.items.push(o);
this.keys.push(null);
}else{
var old = this.map[key];
if(old){
return this.replace(key, o);
}
this.length++;
this.items.push(o);
this.map[key] = o;
this.keys.push(key);
}
this.fireEvent("add", this.length-1, o, key);
return o;
},
。。。。。。
})
为什么extend中的add等函数,不写在构造函数里呢? |
|
| 返回顶楼 | |
|
最后更新时间:2007-12-14
都放到构造函数里,构造函数会变得多大啊?一堆代码会好理解吗?
|
|
| 返回顶楼 | |
|
最后更新时间:2007-12-14
LZ上理解不完全,建议看下Ext.extend方法源代码就会明白了,实际上这样写是为了add之类的函数能override父类的原型链
Ext.extend只对原型链上的方法进行继承,意味如果在构造函数里直接写方法则会导致子类不能继承这些方法 当然也意味着在构造函数里直接写的方法无法被override,可以运行下面的示例: A = function(){ this.method = function(){ alert("A.inner method") } } B = function(){ } B.prototype.method = function(){ alert("B.prototype method") } Ext.extend(A,B,{}) var a = new A() a.method() -------------------------------- 结果:显示A.inner method,导致extend后无法override同名方法 ================================================== A = function(){ } A.prototype.method = function(){ alert("A.prototype method") } B = function(){ } B.prototype.method = function(){ alert("B.prototype method") } Ext.extend(A,B,{}) var a = new A() a.method() ------------------------------------------------ 结果:显示B.prototype method,正确extend并override同名方法 ==================================================== 如果上列只改 Ext.extend(A,B,{ method:function(){ alert("new override method") } }) ----------------------------------------------------- 显示:new override method,override有效 注意:以上的继承概念只针对Ext.extend而言,产生这个问题的原因建议查看js对象调用函数的顺序的相关资料 |
|
| 返回顶楼 | |
|
最后更新时间:2007-12-14
Sean220说得不错!大概明白了。十分感谢!
Sean220 写道 当然也意味着在构造函数里直接写的方法无法被override,可以运行下面的示例: A = function(){ this.method = function(){ alert("A.inner method") } } B = function(){ } B.prototype.method = function(){ alert("B.prototype method") } Ext.extend(A,B,{}) var a = new A() a.method() -------------------------------- 结果:显示A.inner method,导致extend后无法override同名方法 ================================================== 在上面这段中,执行Ext.extend(A,B,{}),怎么没有将B的方法,覆盖A的同名方法呢? |
|
| 返回顶楼 | |
|
最后更新时间:2007-12-15
Ext.extend的时候,只是把B.method方法'copy'到了A.prototype.method.而对象查找好method方法按顺序先找到的是inner method,因此不会执行prototype链上的method方法
|
|
| 返回顶楼 | |








