admin管理员组文章数量:1183204
I am trying to understand better the use of that and this in JavaScript. I am following Douglas Crockford's tutorial here: .html but I am confused regarding a couple of things. I have given an example below, and I would like to know if I am making a correct use of them:
function ObjectC()
{
//...
}
function ObjectA(givenB)
{
ObjectC.call(this); //is the use of this correct here or do we need that?
var aa = givenB;
var that = this;
function myA ()
{
that.getA(); //is the use of that correct or do we need this?
}
this.getA = function() //is the use of this correct?
{
console.log("ObjectA");
};
}
function ObjectB()
{
var that = this;
var bb = new ObjectA(that); //is the use of that correct or do we need this?
this.getB = function()
{
return bb;
};
that.getB(); //is the use of that correct or do we need this?
}
Note this is just an example.
I am trying to understand better the use of that and this in JavaScript. I am following Douglas Crockford's tutorial here: http://javascript.crockford.com/private.html but I am confused regarding a couple of things. I have given an example below, and I would like to know if I am making a correct use of them:
function ObjectC()
{
//...
}
function ObjectA(givenB)
{
ObjectC.call(this); //is the use of this correct here or do we need that?
var aa = givenB;
var that = this;
function myA ()
{
that.getA(); //is the use of that correct or do we need this?
}
this.getA = function() //is the use of this correct?
{
console.log("ObjectA");
};
}
function ObjectB()
{
var that = this;
var bb = new ObjectA(that); //is the use of that correct or do we need this?
this.getB = function()
{
return bb;
};
that.getB(); //is the use of that correct or do we need this?
}
Note this is just an example.
Share Improve this question edited Jul 8, 2014 at 14:37 FranXh asked Jul 8, 2014 at 14:30 FranXhFranXh 4,77120 gold badges60 silver badges78 bronze badges 7 | Show 2 more comments4 Answers
Reset to default 24this
in JavaScript always refers to current object, method of which was called. But sometimes you need to access this
of your object in deeper. For example, in callbacks. Like so:
function MyClass() {
this.a = 10;
this.do = function() {
http.get('blablabla', function(data) {
this.a = data.new_a;
});
};
}
It will not work, because this
in callback may refer to http
, to some dom element or just window(which is really common). So, it is common solution to define self
or that
, an alias for this
or your object, so you can refer it anywhere inside.
function MyClass() {
var self = this;
this.a = 10;
this.do = function() {
http.get('blablabla', function(data) {
self.a = data.new_a;
});
};
}
This should give you vision why it is used and how it should be used.
There is no other reasons(currect me if I'm wrong) to create special variable, you can use this
to send your object to other objects and do things, many assignments, such logic, wow...
ObjectC.call(this); //is the use of this correct here or do we need that?
The first thing you need to understand is how the this
keyword works. It's value depends on how the function/method/constructor is called.
In this case, function ObjectA
is a constructor, so you can just use this
inside the code of it. In fact, with var that = this;
you declare them to be absolutely identical (unless you use that
before assigning to it).
function myA() { that.getA(); //is the use of that correct or do we need this? }
Again, it depends on how the function is called - which you unfortunately have not show us. If if was a method of the instance, this
would have been fine; but but it seems you will need to use that
.
this.getA = function() //is the use of this correct?
As stated above, using that
would not make any difference.
var bb = new ObjectA(that) //is the use of that correct or do we need this? var that = this;
that
is undefined
when it is used here. And it would be supposed to have the same value as this
anyway. Better use this
.
that.getB(); //is the use of that correct or do we need this?
Again, both have the same effect. But since you don't need that
, you should just use this
.
Everything is correct except for :
function ObjectB()
{
var bb = new ObjectA(that) //this is wrong
var that = this;
this.getB = function()
{
return bb;
};
that.getB();
}
You are missing ;
and that
isn't declare.
You need that
(in your case, this is the variable name you use) when you want to use this in another scope :
function ObjectB()
{
var that = this;
// here 'this' is good
function()
{
// Here 'this' doesn't refer to the 'this' you use in function ObjectB()
// It's not the same scope
// You'll need to use 'that' (any variable from the ObjectB function that refers to 'this')
};
// Here 'that' = 'this', so there is no difference in using one or another
}
What "that" is in this context is simply a variable that is equal to "this". That means saying "that" is exactly the same as saying "this", which makes in unnecessarily complicating.
This code:
var that=this;
that.getA();
Will yield the same result as this code:
this.getA();
Having a variable to represent "this" just complicates things when you can just say "this".
本文标签: JavaScript that vs thisStack Overflow
版权声明:本文标题:JavaScript: that vs this - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738282166a2072778.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
that
is not a keyword, but an ordinary variable name. – Bergi Commented Jul 8, 2014 at 14:34that
is used to save the value ofthis
becausethis
of getB function is not the same than ObjectB – guy777 Commented Jul 8, 2014 at 14:36this
is a keyword,that
is just some variable name (it could besausages
). In ObjectB you attempt to passthat
to ObjectA before you have definedthat
. In all of your examples, you could eliminate the use ofthat
entirely and usethis
. The only reason to usethat
is if your usage will be in a scope wherethis
points to something else. – James Commented Jul 8, 2014 at 14:36