PDA

View Full Version : Use of me vs this ...



keckeroo
5 Sep 2011, 3:39 PM
I'm wondering of someone could help clarify the use of me over this I see used more and more in Sencha source code. Is there a benefit to it and if so - it would be interesting if someone could explain to me why me is better than just this ...

Following example taken from ListPagingPlugin ...



onPagingTap : function(e) {
var me = this;
if (!me.loading) {
me.loading = true;
me.list.store.nextPage();
me.el.addCls('x-loading');
}
},


vs



onPagingTap : function(e) {
if (!this.loading) {
this.loading = true;
this.list.store.nextPage();
this.el.addCls('x-loading');
}
},]

AndreaCammarata
5 Sep 2011, 11:39 PM
Hi keckeroo.
I assure you that there's no benefit using me instead of this.
In the example you posted, the developer has probabilly forgot to remove the "me" declaration after apporting some changes inside the "onPagingTap" function.
In fact, in this case, there's no difference between using "this" instead of "me".
However, the "me" variable is often used to set the right scope inside a function where's no other way to do that.
Take a look at the "onLoadComplete" function of the Ext.plugins.PullRefreshPlugin:




onLoadComplete: function() {
var me = this;

if (this.isLoading) {
this.isLoading = false;

this.setViewState('pull');
this.updatedEl.setHTML(Ext.util.Format.date(this.lastUpdated, "m/d/Y h:iA"));

setTimeout(function() {
me.scroller.updateBoundary(me.snappingAnimationDuration);
}, 100);
}
}


As you can see inside the "setTimeout" block, the developer use the variable "me" because "this", used inside the setTimeout function, doesn't return the plugin object like you expect but the DOMWindow object.

Hope this helps.

mitchellsimoens
6 Sep 2011, 5:40 AM
I assure you that there's no benefit using me instead of this.

Sure there is. Once you get over the number of characters "var me = this;" uses, you can now save a few bits which is always welcome in a mobile environment.

Also, we are talking milliseconds maybe, but "this" is a reference so "caching" "this" onto a variable for that method will save some lookup cycles.

AndreaCammarata
6 Sep 2011, 5:54 AM
Sure there is. Once you get over the number of characters "var me = this;" uses, you can now save a few bits which is always welcome in a mobile environment.

Also, we are talking milliseconds maybe, but "this" is a reference so "caching" "this" onto a variable for that method will save some lookup cycles.

Yes, true, it's just a question of microseconds, and I agree on the fact that in a mobile enviroment this can be really usefull, but mitchell, be honest, in the "onPaginTap" example posted by keckeroo would you use the "me" variable? It doesn't have too much sense :). If so, we should do the same for every function inside our custom components / plugins. Don't you agree? However it's good you pointed out that is same way there are some benefits in using "me" instead of "this".

mitchellsimoens
6 Sep 2011, 5:59 AM
In that example, no, may not make sense but just wanted to give a complete example and say what the Sencha Coding style is to use me.

AndreaCammarata
6 Sep 2011, 6:07 AM
In that example, no, may not make sense but just wanted to give a complete example and say what the Sencha Coding style is to use me.

Good to know it ;)