JavaScript:那个 vs 这个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24634484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:08:54  来源:igfitidea点击:

JavaScript: that vs this

javascriptthis

提问by FranXh

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.htmlbut 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:

我试图更好地理解在 JavaScript 中 this 和 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?


 }

Note this is just an example.

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

采纳答案by Bergi

ObjectC.call(this); //is the use of this correct here or do we need that?
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 thiskeywordworks. It's value depends on how the function/method/constructor is called.

你需要了解的第一件事是如何的this关键字作品。它的值取决于函数/方法/构造函数的调用方式。

In this case, function ObjectAis a constructor, so you can just use thisinside the code of it. In fact, with var that = this;you declare them to be absolutely identical (unless you use thatbefore assigning to it).

在这种情况下,function ObjectA是一个构造函数,所以你可以this在它的代码中使用。事实上,与var that = this;您声明它们完全相同(除非您that在分配之前使用)。

function myA() {
    that.getA(); //is the use of that correct or do we need this?
}
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, thiswould have been fine; but but it seems you will need to use that.

同样,这取决于函数的调用方式 - 不幸的是,您没有向我们展示。如果 if 是实例的方法,那this就没问题了;但似乎您将需要使用that.

this.getA = function() //is the use of this correct?
this.getA = function() //is the use of this correct?

As stated above, using thatwould not make any difference.

如上所述,使用that不会有任何区别。

var bb = new ObjectA(that) //is the use of that correct or do we need this?
var that = this;
var bb = new ObjectA(that) //is the use of that correct or do we need this?
var that = this;

thatis undefinedwhen it is used here. And it would be supposed to have the same value as thisanyway. Better use this.

thatundefined在这里使用的时候。它应该具有与this无论如何相同的值。更好地使用this

that.getB(); //is the use of that correct or do we need 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 needthat, you should just use this.

同样,两者具有相同的效果。但既然你不需要that,你应该只使用this.

回答by dt0xff

thisin JavaScript always refers to current object, method of which was called. But sometimes you need to access thisof your object in deeper. For example, in callbacks. Like so:

this在 JavaScript 中总是指当前对象,其方法被调用。但有时您需要this更深入地访问您的对象。例如,在回调中。像这样:

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

It will not work, because thisin callback may refer to http, to some dom element or just window(which is really common). So, it is common solution to define selfor that, an alias for thisor your object, so you can refer it anywhere inside.

它不会工作,因为this在回调中可能会引用http, 某些 dom 元素或只是窗口(这很常见)。因此,定义selfor that、or的别名是this您的对象的常见解决方案,因此您可以在内部的任何位置引用它。

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 visionwhyit is used and howit should be used.

这应该让您了解为什么使用它以及应该如何使用它。

There is no other reasons(currect me if I'm wrong) to create special variable, you can use thisto send your object to other objects and do things, many assignments, such logic, wow...

没有其他原因如果我错了,请纠正我)创建特殊变量,您可以使用this将您的对象发送到其他对象并执行操作,许多赋值,这样的逻辑,哇...

回答by Elfayer

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 thatisn't declare.

你失踪了;that没有申报。



You need that(in your case, this is the variable name you use) when you want to use this in another scope :

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
     }

回答by RageCage

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".

当你可以只说“这个”时,有一个变量来代表“这个”只会使事情复杂化。