4

I came across this many times in a new code base that I'm looking at and was wondering is there is any proper reasoning behind it?

4

3 回答 3

2

您可以使用var that = this;is 命令保留对当前this对象的引用,稍后this将指向其他对象。

示例(取自此处):

$('#element').click(function(){
    // this is a reference to the element clicked on

    var that = this;

    $('.elements').each(function(){
        // this is a reference to the current element in the loop
        // that is still a reference to the element clicked on
    });
});
于 2013-05-28T14:28:06.047 回答
1

有时this,JavaScript 中的含义会根据作用域而变化。 this构造函数内部的含义与this函数内部不同。这是一篇关于它的好文章

于 2013-05-28T14:26:21.007 回答
0

如果您想在特定函数调用范围之外/内部访问“this”,其中“this”可能已更改。只是我能想到的一个例子。

于 2013-05-28T14:27:35.123 回答