Javascript 将此传递给window.onscroll函数
如何将Javascript 将此传递给window.onscroll函数,javascript,ecmascript-6,this,Javascript,Ecmascript 6,This,如何将此传递给分配给我的窗口.onscroll事件的函数 我试图在满足特定条件时触发myFunction()。我需要检查此条件onscroll init() { window.onscroll = function() { if(this.currentItemCount() > this.totalElements){ this.totalElements = this.currentItemCount(); this.myFunc
此
传递给分配给我的窗口.onscroll
事件的函数
我试图在满足特定条件时触发myFunction()
。我需要检查此条件onscroll
init() {
window.onscroll = function() {
if(this.currentItemCount() > this.totalElements){
this.totalElements = this.currentItemCount();
this.myFunction();
}
};
}
但是,我得到一个错误,
this.currentItemCount()
不是一个函数。我知道我需要将此传递给窗口。onscroll
,但我无法找出正确的语法。您可以使用this=此
构造。()
或者更好地使用arrow函数,该函数从包装上下文中保留此(需要ES6支持或transpiler):
您可以使用that=this
构造。()
或者更好地使用arrow函数,该函数从包装上下文中保留此(需要ES6支持或transpiler):
您可以尝试以下方法:
init() {
var self = this;
window.onscroll = function() {
if(self.currentItemCount() > self.totalElements){
self.totalElements = self.currentItemCount();
self.myFunction();
}
};
}
此
在内部作用域中不可用,但将提供self
。您可以尝试以下方法:
init() {
var self = this;
window.onscroll = function() {
if(self.currentItemCount() > self.totalElements){
self.totalElements = self.currentItemCount();
self.myFunction();
}
};
}
这个
在内部范围内不可用,但是可以使用self
。使用类似于goUsing的方法听起来像是goThanks的方法,arrow函数是我想要的语法,只是不能完全正确。谢谢,arrow函数是我想要的语法,只是做得不太对。
init() {
var self = this;
window.onscroll = function() {
if(self.currentItemCount() > self.totalElements){
self.totalElements = self.currentItemCount();
self.myFunction();
}
};
}