【www.bbyears.com--extjs】
今天在看其他人代码的时候发现调用的函数和自己想像的不一样。所以认真看了一下代码,再结合之前js关于call的笔记。知道了其中的原因
代码如下 Son.superclass.initComponent.call(this);这里的call就是调用"Son"父类的initComponent函数,并把this传进去,这个this是Son的实例对象。
下面用一个js代码解释一下这个call 是怎样的
function Dog(name){
this.name = name;
}
function test(){
alert(this.name);
}
var dog = new Dog("lil");
test.call(dog);//这句话相当于dog.test();所以test()里面的this是dog,即this.name是dog.name,即弹出"lil"
如上面代码所示,test.call(dog)等价于dog.test();
下面举ExtJs代码的例子
Son = Ext.extend(Father,{
initComponent : function() {
Son.superclass.initComponent.call(this);//这里调用父类的initComponent方法,即Father的initComponent方法,并把this,Son的实例对象做为参数传过去
},
test : function() {
alert("test in Son");
}
}
Father = Ext.extend(Ext.Panel,{
initComponent : function() {
Father.superclass.initComponent.call(this);
this.test();//这里的this是Son的实例对象,相当于Son son = new Son();son.test();
//所以调用的是子类的test方法而不是父类的test方法,我之前以为是调用父类的test方法
},
test : function() {
alert("test in Father");
}
}