JavaScript: that与this的区别

5

我正在努力更好地理解JavaScript中that和this的用法。我正在遵循Douglas Crockford的教程,链接在此:http://javascript.crockford.com/private.html 但是有几个问题让我感到困惑。下面是我的一个例子,我想知道我是否正确使用了它们:

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?


 }

请注意,这只是一个例子。

1
简单:that不是关键字,而是普通的变量名。 - Bergi
2
在投票关闭问题之前,请解释您不喜欢它的原因,这样我就有机会在将来改进。 - FranXh
'that' 用于保存 this 的值,因为 getB 函数中的 this 与 ObjectB 不同。 - guy777
@James - 我的错误,已经修复 - FranXh
http://alistapart.com/article/getoutbindingsituations - Marcelo Aymone
显示剩余2条评论
4个回答

24

this 在 JavaScript 中始终指向 当前调用方法的对象。但有时您需要更深入地访问对象中的this。例如,在回调函数中。像这样:

function MyClass() {
    this.a = 10;
    this.do = function() {
        http.get('blablabla', function(data) {
            this.a = data.new_a;
        });
    };
}

这样做不起作用,因为回调函数中的this可能指向http、某些DOM元素或者仅仅是窗口(这很常见)。因此,通常的解决方案是定义selfthat,它们是this或您对象的别名,这样您就可以在任何地方引用它了。

function MyClass() {
    var self = this;
    this.a = 10;
    this.do = function() {
        http.get('blablabla', function(data) {
            self.a = data.new_a;
        });
    };
}

这会让你理解为什么要使用它以及如何使用它。

没有其他原因(如果我说错了请纠正我)创建特殊变量,你可以使用this将你的对象发送给其他对象并执行一些操作,多个赋值,逻辑运算等等...


3

除了以下内容外,一切都正确:

function ObjectB()
 {
    var bb = new ObjectA(that) //this is wrong
    var that = this;

    this.getB = function()
    {
        return bb;
    };

    that.getB();
 }

您缺少分号;,并且that未被声明。
当您想在另一个范围中使用此变量名称时,您需要that(在您的情况下)。
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
     }

3
ObjectC.call(this); //is the use of this correct here or do we need that?
你需要先了解的是this关键字的作用。它的值取决于函数/方法/构造函数的调用方式。
在这种情况下,function ObjectA是一个构造函数,所以你可以在其代码中直接使用this。事实上,通过var that = this;声明它们是完全相同的(除非你在分配之前使用that)。
function myA() {
    that.getA(); //is the use of that correct or do we need this?
}

再次强调,这取决于函数的调用方式 - 但是您很不幸没有展示给我们。如果它是实例的方法,this 就可以使用; 但是看起来你需要使用 that

this.getA = function() //is the use of this correct?
如上所述,使用that不会有任何影响。
var bb = new ObjectA(that) //is the use of that correct or do we need this?
var that = this;

that在这里使用时是未定义的。而且它本来应该与this具有相同的值。最好使用this

that.getB(); //is the use of that correct or do we need this?

再次强调,两者的作用是相同的。但由于您不 需要 那个,所以您应该只使用这个


0
在这个上下文中,“that”指的只是一个等于“this”的变量。这意味着说“that”和说“this”是完全一样的,这使得它变得不必要地复杂。
这段代码:
var that=this;
that.getA();

将产生与此代码相同的结果:

this.getA();

当你可以直接使用"this"时,用一个变量来表示它只会让事情变得更加复杂。


-1 没有抓住重点。有时需要引用类自身对象的函数。请阅读其他答案。这并非是不必要的。 - user3685285

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接