PDA

View Full Version : Base.js - override example question



LesJ
7 Feb 2012, 8:01 AM
See the override example from Base.js.

The constructor returns an instance. Is this required? It works fine if the constructor has no return value.


Ext.define('My.Cat', {
constructor: function() {
alert("I'm a cat!");
}
});


Ext.define('My.CatOverride', {
override: 'My.Cat',
constructor: function() {
alert("I'm going to be a cat!");

var instance = this.callParent(arguments);

alert("Meeeeoooowwww");

return instance;
}
});

Also, the default constructor returns this, but I don't see that subclasses (e.g. components) return this, so why does the default constructor return this? Should the default constructor be an empty function?


// Default constructor, simply returns `this`
constructor: function() {
return this;
},

Ext.ClassManager has multiple inline examples where the constructor returns this.

mitchellsimoens
7 Feb 2012, 12:14 PM
if you do


new Ext.Component({...});

It returns the instance which is what constructor does.

LesJ
7 Feb 2012, 12:36 PM
if you do


new Ext.Component({...});

It returns the instance which is what constructor does.

Is this (simpler code) the correct way to override My.Cat?


Ext.define('My.Cat', {
constructor: function() {
alert("I'm a cat!");
}
});


Ext.define('My.CatOverride', {
override: 'My.Cat',
constructor: function() {
alert("I'm going to be a cat!");

this.callParent(arguments);

alert("Meeeeoooowwww");
}
});

new My.Cat()

mitchellsimoens
7 Feb 2012, 12:42 PM
If you are overriding, you should use callOverridden instead of callParent. Not sure if that makes much of a difference though and has been deprecated in ST2

LesJ
7 Feb 2012, 12:57 PM
If you are overriding, you should use callOverridden instead of callParent. Not sure if that makes much of a difference though and has been deprecated in ST2

This is an inline Sencha example (not my code) and it calls callParent, not callOverriden.

I'm also confused about this inline example from ClassManager. I modified this example. I'm not returning this from the constructor and it works just as well. Is returning this from constructor required?

Please clarify if I should return this from the constructor when I extend or override a class.


Ext.define('Person', {
name: 'Unknown',

constructor: function(name) {
if (name) {
this.name = name;
}

// return this;
},

eat: function(foodType) {
alert("I'm eating: " + foodType);

return this;
}
});

var aaron = new Person("Aaron");
aaron.eat("Sandwich"); // alert("I'm eating: Sandwich");


Ext.define('Developer', {
extend: 'Person',

constructor: function(name, isGeek) {
this.isGeek = isGeek;

// Apply a method from the parent class' prototype
this.callParent([name]);

// return this;
},

code: function(language) {
alert("I'm coding in: " + language);

this.eat("Bugs");

return this;
}
});

var jacky = new Developer("Jacky", true);
jacky.code("JavaScript")

mitchellsimoens
7 Feb 2012, 1:05 PM
I personally never return anything in my constructors and have had zero issues in creating extensions or overrides.

LesJ
7 Feb 2012, 1:15 PM
I personally never return anything in my constructors and have had zero issues in creating extensions or overrides.

OK I won't let these examples bother me :)