循环绑定中的绝对引用Javascript变量

循环绑定中的绝对引用Javascript变量,javascript,jquery,javascript-events,Javascript,Jquery,Javascript Events,我已经创建了下面的函数来绑定到我页面中所有类型为“.wrapper”的类。我的问题是,当我触发这些事件时,var‘this’不再指向绑定它的特定循环,而是指向循环绑定的最终迭代中使用的‘this’。在我理解var是如何存储在绑定中,而不是作为一个值存储在指针中时,这似乎是一个错误。有谁能帮我展示一下如何使这些绑定与它们的对象保持关联 谢谢Devin var pullUpEl, pullUpOffset, generatedCount = 0, index = 0, wrappers = [];

我已经创建了下面的函数来绑定到我页面中所有类型为“.wrapper”的类。我的问题是,当我触发这些事件时,var‘this’不再指向绑定它的特定循环,而是指向循环绑定的最终迭代中使用的‘this’。在我理解var是如何存储在绑定中,而不是作为一个值存储在指针中时,这似乎是一个错误。有谁能帮我展示一下如何使这些绑定与它们的对象保持关联

谢谢Devin

var pullUpEl, pullUpOffset, generatedCount = 0, index = 0, wrappers = [];

$('.wrapper').each(function () {
    if ($(this).hasClass('tooltip')) {
        pullUpEl = $(this)[0].getElementsByTagName("div")[0].getElementsByTagName("div")[0];
        pullUpOffset = pullUpEl.offsetHeight;

        wrappers[index] = new iScroll($(this).attr('id'), {
            useTransition: true,
            fadeScrollbar: true,
            hideScrollbar: false,
            hScrollbar: false,
            //snap: 'li',
            onRefresh: function () {
                if (pullUpEl.className.match('loading')) {
                    pullUpEl.className = '';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
                }
            },
            onScrollMove: function () {
                if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                    pullUpEl.className = 'flip';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
                    this.maxScrollY = this.maxScrollY;
                } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                    pullUpEl.className = '';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
                    this.maxScrollY = pullUpOffset;
                }
            },
            onScrollEnd: function () {
                if (pullUpEl.className.match('flip')) {
                    pullUpEl.className = 'loading';
                    pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';
                    pullUpAction(this, pullUpEl.parentNode.getElementsByTagName("ul")[0]);
                }
            }
        });
    } else {
        wrappers[index] = new iScroll($(this).attr('id'));
    }
    index++;
});
var pullUpEl,pullUpOffset,generatedCount=0,index=0,wrappers=[];
$('.wrapper')。每个(函数(){
if($(this).hasClass('tooltip')){
pullUpEl=$(this)[0]。getElementsByTagName(“div”)[0]。getElementsByTagName(“div”)[0];
pullUpOffset=pullUpEl.offsetHeight;
wrappers[index]=new iScroll($(this).attr('id'){
对,,
fadeScrollbar:没错,
hideScrollbar:false,
hScrollbar:错,
//抓拍:"李",,
onRefresh:function(){
if(pullUpEl.className.match('load')){
pullUpEl.className='';
pullUpEl.querySelector('.pullUpLabel').innerHTML='Pull-up-to-load-more…';
}
},
onScrollMove:函数(){
if(this.y<(this.maxScrollY-5)和&!pullUpEl.className.match('flip')){
pullUpEl.className='flip';
pullUpEl.querySelector('.pullUpLabel').innerHTML='releasetoresh…';
this.maxScrollY=this.maxScrollY;
}else if(this.y>(this.maxScrollY+5)和&pullUpEl.className.match('flip')){
pullUpEl.className='';
pullUpEl.querySelector('.pullUpLabel').innerHTML='Pull-up-to-load-more…';
this.maxScrollY=上拉偏移量;
}
},
onScrollEnd:函数(){
if(pullUpEl.className.match('flip')){
pullUpEl.className='loading';
pullUpEl.querySelector('.pullUpLabel').innerHTML='Loading…';
pullUpAction(这是pullUpEl.parentNode.getElementsByTagName(“ul”)[0]);
}
}
});
}否则{
wrappers[index]=newiscroll($(this.attr('id'));
}
索引++;
});

非正式地说,
这个
的计算结果是第一个包含
函数的词汇上下文(隐式参数)。您可以通过以下方式捕获它:

function f() {
    var self = this;
    function g() {
        // here "self" evaluates to the context of f, "this" evaluates to the one of g
    }
}
要在浏览器控制台中运行,请尝试:

function f() {
    var self = this;
    return function g() {
        console.log(String(self));
        console.log(String(this));
    }
}

f.apply(1).apply(2)
有关血淋淋的详细信息,请参见第11.1.1节:

this
关键字的计算结果为 当前执行上下文


本标准规定,
ThisBinding
仅在输入函数代码或输入
eval
code时更改。

非正式地,
计算为第一个以词汇形式封装的
函数的上下文(隐式参数)。您可以通过以下方式捕获它:

function f() {
    var self = this;
    function g() {
        // here "self" evaluates to the context of f, "this" evaluates to the one of g
    }
}
要在浏览器控制台中运行,请尝试:

function f() {
    var self = this;
    return function g() {
        console.log(String(self));
        console.log(String(this));
    }
}

f.apply(1).apply(2)
有关血淋淋的详细信息,请参见第11.1.1节:

this
关键字的计算结果为 当前执行上下文


本标准规定,
ThisBinding
仅在输入功能代码或输入
eval
代码时更改。

这不是
的问题,这是
pullUpEl
的问题。您将此设置为全局,因此它的最终值是
.each()
块中的最后一个元素。运行
onRefresh
onScrollMove
等函数时,它们不会在
pullUpEl
实际发生变化的上下文中运行。因此,本质上,无论哪个元素触发此操作,每次都会在循环的最后一个元素上运行所有更改。

这不是
的问题,而是
pullUpEl的问题。您将此设置为全局,因此它的最终值是
.each()
块中的最后一个元素。运行
onRefresh
onScrollMove
等函数时,它们不会在
pullUpEl
实际发生变化的上下文中运行。所以,从本质上说,无论哪个元素触发此操作,每次都会在循环的最后一个元素上运行所有更改。

向上投票人:这是答案,95%错误,请不要向上投票?请随意教我。首先,你可以尝试一下你的代码,看看你在注释中的假设是错误的。“隐式参数”的正确名称是“上下文”。它不是指函数对象,而是指它的上下文。@Bergi上下文和“函数中的
this
的值”的意思是一样的,所以说
this
总是指上下文就像说
this
总是指
this
的值一样:XTo投票人:这是答案,95%是错误的,请不要太过分?请随意教我。首先,你可以尝试一下你的代码,看看你在注释中的假设是错误的。“隐式参数”的正确名称是“上下文”。它不是指函数对象,而是指它的上下文。@Bergi上下文和“函数中
this
的值”的意思是一样的,所以说
this
总是指上下文就像说
this
总是指
this
的值一样:XThanks Chris。我这方面真是愚蠢的疏忽!如果你觉得有挑战性,我还有一个非常棘手的问题要解决;)如果你能看一眼,我将不胜感激!再次感谢!为DevinThanks Chris干杯。我这方面真是愚蠢的疏忽!如果你觉得有挑战性,我还有一个非常棘手的问题要解决;)如果你能