No, it's not true, I've never even heard of such a thing.
All you are doing when you do
var _this = this;
Is creating a variable to point to the object in memory that is referred to when you use this
. Whether you say:
this.name = "Petter";
or
_this.name = "Petter";
You are still assigning a property to the same object. The way you reference that object (_this
or this
) makes no difference.
EDIT
You often need to get a reference to this
when you want to use this
in a different scope (setTimeout's come to mind as a good example).
var MyClass = function() {
setTimeout(function() { this.myMethod(); },100);
};
MyClass.prototype.myMethod = function() {
console.log('hi there');
}
var myObject = new MyClass();
In the above code, you would get an error because when the setTimeout function gets executed, it does so in the global scope where this === window
and you have no function called myMethod()
on the window
object.
To correct that, you would do this:
var MyClass = function() {
var self = this;
setTimeout(function() { self.myMethod(); },100);
};
MyClass.prototype.myMethod = function() {
console.log('hi there');
}
var myObject = new MyClass();
Even though your setTimeout function is executed in the global scope, the variable self
actual points to an instance of MyClass
(or this
) because you did self = this
(and because JavaScript is lexically scoped.
Also, and this is just personal preference, you'll quite often see this:
var self = this;
instead of
var _this = this;
Not a big deal, but it's just a convention I thought might be worth mentioning.