JavaScript:那个 vs 这个 [英] JavaScript: that vs this

查看:27
本文介绍了JavaScript:那个 vs 这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地理解在 JavaScript 中使用 that 和 this.我正在关注 Douglas Crockford 的教程:http://javascript.crockford.com/private.html但我对一些事情感到困惑.我在下面给出了一个例子,我想知道我是否正确使用它们:

function ObjectC(){//...}函数对象A(给定B){ObjectC.call(this);//这里的使用是正确的还是我们需要它?var aa = givenB;var that = this;函数 myA(){那.getA();//使用正确还是我们需要这个?}this.getA = function()//这个用法正确吗?{console.log("ObjectA");};}函数 ObjectB(){var that = this;var bb = new ObjectA(that);//使用那个正确还是我们需要这个?this.getB = function(){返回 bb;};那.getB();//使用那个正确还是我们需要这个?}

注意这只是一个例子.

解决方案

ObjectC.call(this);//这里的使用是正确的还是我们需要它?

您需要了解的第一件事是this 关键字 有效.它的值取决于函数/方法/构造函数的调用方式.

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

<块引用>

function myA() {那.getA();//使用那个正确还是我们需要这个?}

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

<块引用>

this.getA = function()//this的使用正确吗?

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

<块引用>

var bb = new ObjectA(that)//使用那个正确还是我们需要这个?var that = this;

that 在这里使用时是 undefined .无论如何,它应该具有与 this 相同的值.最好使用this.

<块引用>

that.getB();//使用那个正确还是我们需要这个?

同样,两者具有相同的效果.但是由于您不需要需要那个,所以您应该只使用this.

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.

解决方案

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.

这篇关于JavaScript:那个 vs 这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆