14

what is the difference between these snippets?:

constructor: function(config) {
    this.initConfig(config);
}

constructor: function(config) {
    this.callParent(arguments);
}

constructor: function(config) {
    this.superclass.constructor.call(this, config);
}

AND, how does it differ to be overriding OTHER methods than constructor?

Paul
  • 9,285
  • 6
  • 29
  • 37

1 Answers1

26

The second and third snippet are functionally equivalent. callParent is just a cleaner and simpler way to invoke the parent's function instead of going through the superclass property.

The first example is completely different. For one, it does not call the parent's constructor. This is usually bad news. The base class usually has some initialization that needs to get done, and forgetting to call the parent for constructor (and initComponent) is a common source of bugs.

initConfig is a convenience method which will:

  • merge your object's initial config with the config that is passed in
  • create getters and setters for all properties in the config
  • apply the config to the object

This is entirely different from invoking the parent constructor. What invoking the parent constructor does is entirely dependent on what it's written to do. Some will use initConfig, some won't, and others will do similar things that initConfig does, but manually.

It's not necessary to use initConfig, and it's even dangerous at times. But that's another story altogether (for Ext 4.1, initConfig looks to be hopefully harmless and not contain the same dangerous side effects that it has in 4.0)

Side Note

Just to point out. In the second and third snippet, the constructor is not needed. If your constructor is only calling the parent's constructor, then you can leave your constructor out altogether.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • thanks, matt. very helpful. could you comment on the difference between the constructor and initialize functions? do they apply to all defined classes? how do they differ? – Paul Apr 12 '12 at 19:26
  • ok, learning here... i subclassed a proxy and see that i can override constructor, initialize, and initConfig. i overrode initConfig in order to modify the config - is that appropriate? it worked ok. also, still curious the diff between initialize and constructor – Paul Apr 12 '12 at 19:42
  • 1
    You probably shouldn't override initConfig. I'd argue don't use initConfig at all. Sencha doesn't use initConfig ever in any of their stuff. The reason why is initConfig can blow up on you in certain situations. `initialize` on `Proxy` is private. Probably best to leave it alone. For Proxies I'd say the best way to initialize it is by subclassing the constructor. – Matt Greer Apr 13 '12 at 02:27
  • 5
    After analyzing some Ext source and test: 1. You have to call `Ext.apply(this, config || {})` instead of `this.callParent(arguments)` if you are not extending any classes, otherwise `Ext.create('class', {config})` will not being applied. 2. Calling `this.callParent(arguments)` is enough if you extend from most of the Ext classes (or your own parent classes) since the parent class that extend from Ext.Base is already calling `Ext.apply(this, config || {})` 3. Most of Ext source call `this.callParent([config])` to explicitly allow config and discard other arguments. – CallMeLaNN Jun 10 '13 at 16:28