之前在解说视频有看过javascript的call。这个要领在ExtJs中是很常用的。 早年在搞ExtJs的时辰老是忽略initComponent内里的一句代码,如下所示。本日在看其他人代码的时辰发明挪用的函数和本身想像的纷歧样。以是当真看了一下代码,再团结之前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'); } } (编辑:河北网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|