Consider the example:
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.app.Application-static-method-getName
Ext.define('My.cool.Class', {
constructor: function() {
alert(this.self.getName()); // alerts 'My.cool.Class'
}
});
My.cool.Class.getName(); // 'My.cool.Class'
What is the self referring to in this example?
How can I, in this documentation, know when to use this and when self and when this.self?
Why this is not working:
this.getName()
or
self.getName()
My thoughts about this are that self refers to the class of the object so the only reason I need to do this is because the getName() method is static so I’m (kinda) not calling it from object, but from class. Am I right? Am I? Ha? Ha? Am I? 😀
this.selfrefers to the class object. It means thatthis.self === My.cool.Class. So you can instantiate newMy.cool.Classobject by invokingnew this.self().The reason why
this.getName()doesn’t work is because in JS static properties/methods are not available in instance.Example:
Also static properties/methods are not available in sub class even in static context.
Example:
The reason why
getNamemethod is available inMy.cool.Classis because there are copied fromExt.Baseclass inExtClass.createmethod (this class is private, it’s not visible in API).