I find this pattern all over the ExtJS source code.
method: function() { var me = this; ... me.someOtherMethod(); }
Why don't they just use this
? Is there some advantage to always often defining me
(outside of not having to type 2 characters)? I can understand if they are trying to maintain the context via closure, but it's done in places where there is no closure at all.
An example from Ext.panel.Panel:
disable: function(silent) { var me = this; if (me.rendered) { me.el.addCls(me.disabledCls); me.el.dom.disabled = true; me.onDisable(); } me.disabled = true; if (silent !== true) { me.fireEvent('disable', me); } return me; },
Value at risk (VaR) is a statistic that quantifies the extent of possible financial losses within a firm, portfolio, or position over a specific time frame.
1. The concept behind this is, to use "that" as function wide global variable when you use closures to access the scope of the object. It's not a good way to do this in object oriented programming but in Javascript it's a quick way to solve such problems.
The Var Keyword This means that if a variable is defined in a loop or in an if statement it can be accessed outside the block and accidentally redefined leading to a buggy program. As a general rule, you should avoid using the var keyword.
Variable naming There are two limitations on variable names in JavaScript: The name must contain only letters, digits, or the symbols $ and _ . The first character must not be a digit.
If your library is doing this in places where there are no embedded closures/callbacks that might have their own value of this
, then this is just a "practice" or "style" or "convention" that they decided to follow in their methods. There is NO programming reason to always do it.
In the specific coding example you have now added to your question, there is no reason I'm aware of other than a common coding style. This code would generate the same result in slightly smaller code:
disable: function(silent) { if (this.rendered) { this.el.addCls(this.disabledCls); this.el.dom.disabled = true; this.onDisable(); } this.disabled = true; if (silent !== true) { this.fireEvent('disable', this); } return this; },
When there is a callback or closure involved, this is often done because there are callbacks used inside the method where they still want a reference to this
, but those callbacks will have their own value of this
so assigning:
var me = this;
or more commonly in other code I've seen:
var self = this;
is a way of retaining access to that object even in a callback that has a different value of this
.
Here's a generic example:
document.getElementById("myButton").onclick = function () { var self = this; // save reference to button object use later setTimeout(function() { // "this" is not set to the button inside this callback function (it's set to window) // but, we can access "self" here self.style.display = "none"; // make button disappear 2 seconds after it was clicked }, 2000); };
One reason is because with minification, me
can become much shorter than this
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With