在ExtJS中调用超类方法的更好的方法 [英] Better way to call superclass method in ExtJS

查看:187
本文介绍了在ExtJS中调用超类方法的更好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过的所有ExtJS文档和示例都建议调用超类方法,如下所示:

  MyApp.MyPanel = Ext.extend (Ext.Panel,{
initComponent:function(){
// do something MyPanel specific here ...
MyApp.MyPanel.superclass.initComponent.call(this);
}
});

我一直在使用这种模式,主要的问题是,当你重命名你的类,那么你也必须将所有调用更改为超类方法。这很不方便,通常我会忘记,然后我必须追踪奇怪的错误。



但是阅读我发现,我可以使用 superclass() super()方法 Ext.extend()添加到原型:

  MyApp.MyPanel = Ext.extend(Ext.Panel,{
initComponent:function(){
// do something MyPanel specific here ...
this.superclass()。initComponent.call(this) ;
}
});

在这段代码中,将MyPanel重命名为别的东西很简单 - 我只需要更改一行。 / p>

但我有疑惑...




  • 我没有看到这一点记录在任何地方,老智慧说,我不应该依赖无证行为。


  • 我没有发现这些 superclass() supr() ExtJS源代码中的方法。为什么要在不使用它们时创建它们?


  • 也许这些方法在一些较旧版本的ExtJS中被使用,但现在已经弃用了?但是,这似乎是一个有用的功能,为什么你会弃用它?




所以,我应该使用这些方法吗

解决方案

确实, supr()没有记录。我一直期待在ExtJS 3.0.0中使用它(Ext的员工在论坛上回复,他们已经在该版本中添加了它),但是看起来很可怕。



它目前没有遍历继承层次结构,而是上升一级,然后被卡在这个级别上,循环不断,并且堆栈(IIRC)。所以,如果你有两个或更多的 supr(),你的应用程序就会中断。我没有在文档和论坛中没有找到有关 supr()的任何有用的信息。



知道维护版本3.0.x,因为我没有得到支持许可证。


All the ExtJS documentation and examples I have read suggest calling superclass methods like this:

MyApp.MyPanel = Ext.extend(Ext.Panel, {
  initComponent: function() {
    // do something MyPanel specific here...
    MyApp.MyPanel.superclass.initComponent.call(this);
  }
});

I have been using this pattern for quite some time and the main problem is, that when you rename your class then you also have to change all the calls to superclass methods. That's quite inconvenient, often I will forget and then I have to track down strange errors.

But reading the source of Ext.extend() I discovered, that instead I could use the superclass() or super() methods that Ext.extend() adds to the prototype:

MyApp.MyPanel = Ext.extend(Ext.Panel, {
  initComponent: function() {
    // do something MyPanel specific here...
    this.superclass().initComponent.call(this);
  }
});

In this code renaming MyPanel to something else is simple - I just have to change the one line.

But I have doubts...

  • I haven't seen this documented anywhere and the old wisdom says, I shouldn't rely on undocumented behaviour.

  • I didn't found a single use of these superclass() and supr() methods in ExtJS source code. Why create them when you aren't going to use them?

  • Maybe these methods were used in some older version of ExtJS but are deprecated now? But it seems such a useful feature, why would you deprecate it?

So, should I use these methods or not?

解决方案

Yes indeed, supr() isn't documented. I've been looking forward to using it in ExtJS 3.0.0 (an Ext staff member replied in the forums, they had added it in that version), but it seems horribly broken.

It currently does not traverse the inheritance hierarchy, but rather go up one level, then gets stuck on this level, loops endlessly and blows up the stack (IIRC). So, if you have two or more supr() in a row, your app will break. I have not found any useful information on supr() in neither the docs nor the forums.

I don't know about the maintenance releases 3.0.x, since I did not get an support license ...

这篇关于在ExtJS中调用超类方法的更好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆