javascript 在javascript中使用“that”关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14871757/
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-26 22:48:42  来源:igfitidea点击:

use of "that" keyword in javascript

javascript

提问by Ayusman

I have few javascript that has used the keyword "that" extensively. I see a lot of posts talking about the javascript keyword "this".

我几乎没有广泛使用关键字“that”的 javascript。我看到很多帖子都在谈论 javascript 关键字“this”。

I wanted to understand the meaning of this key word in javascript context and it's visibility/scope.

我想了解这个关键字在 javascript 上下文中的含义以及它的可见性/范围。

Something like

就像是

that.someFunctionaName(someParameter)

What does it mean?

这是什么意思?

I understand the keyword "this" always points to the owner of the current object.

我理解关键字“this”总是指向当前对象的所有者。

回答by BenM

thatis not a keyword in JavaScript. I suspect the code that you have is using something in the class to define an instance of itself. For example:

that不是 JavaScript 中的关键字。我怀疑您拥有的代码正在使用类中的某些内容来定义其自身的实例。例如:

function myClass()
{
    var that = this;
}

By doing this, you can ensure you're referencing the object, and not another element. For example, consider the following sample:

通过这样做,您可以确保您引用的是对象,而不是另一个元素。例如,请考虑以下示例:

function myClass()
{
    var that = this;
    $('.myele').click(function() {  

        // 'this' refers to the element that was clicked.
        // 'that' still refers to the myClass() object.
    });
}