Javascript 合并此内部回调
我有JavaScript中的OOP函数,在这里我使用带有回调的库 代码如下所示:Javascript 合并此内部回调,javascript,node.js,Javascript,Node.js,我有JavaScript中的OOP函数,在这里我使用带有回调的库 代码如下所示: $('body').mCustomScrollbar({ theme: 'minimal', callbacks: { whileScrolling: function() { console.log(this.mcs.draggerTop); } } }); 在回调函数(whileScrolling)中,我有this.mcs(由库生成)。但是我没有访问OOP函数变量的权限
$('body').mCustomScrollbar({
theme: 'minimal',
callbacks: {
whileScrolling: function() {
console.log(this.mcs.draggerTop);
}
}
});
在回调函数(whileScrolling)中,我有this.mcs(由库生成)。但是我没有访问OOP函数变量的权限。
如果我想访问它们,我必须这样做
whileScrolling: function() {
console.log(this.mcs.draggerTop);
}.bind(this);
如果我这样做,我就可以通过这个来访问所有的东西。变量(在OOP函数中),但我在旧的内部回调中释放了所有变量,所以我不再有this.mcs.draggedTop
有没有办法“合并”这个es(上一个+新的)?试试看
$('body').mCustomScrollbar({
theme: 'minimal',
self:this,
callbacks: {
whileScrolling: function() {
console.log(self.localVariable);
}
}
})
试一试
将外部
this
的值分配给一个变量,并在回调中使用该变量:
var outerThis = this; // the outer this
$('body').mCustomScrollbar({
theme: 'minimal',
callbacks: {
whileScrolling: function() {
// here this is the inner this
// and outerThis is the outer this
console.log(this.mcs.draggerTop);
}
}
});
将外部
this
的值分配给一个变量,并在回调中使用该变量:
var outerThis = this; // the outer this
$('body').mCustomScrollbar({
theme: 'minimal',
callbacks: {
whileScrolling: function() {
// here this is the inner this
// and outerThis is the outer this
console.log(this.mcs.draggerTop);
}
}
});
这是无效的javascriptYou不能在对象文字中声明变量我没有意识到它是对象…:(这是无效的javascriptYou不能在对象文字中声明变量我没有意识到它是对象…:(