绑定' this'在JavaScript函数中
我有一个函数:
interval(duration, fn) {
var _this = this
this.baseline = undefined
this.run = function(){
if(_this.baseline === undefined){
_this.baseline = new Date().getTime()
}
fn()
var end = new Date().getTime()
_this.baseline += duration
var nextTick = duration - (end - _this.baseline)
if(nextTick<0){
nextTick = 0
}
_this.timer = setTimeout(function(){
_this.run(end)
}, nextTick)
}
this.stop = function(){
clearTimeout(_this.timer)
}
}
现在,当我这样调用此功能时:
var timer = new this.interval(this.tempo, function(){
this.audio.pause()
this.playBackground()
})
我需要以某种方式绑定“ this”,以便可以访问它,以便我可以调用this.audio.pause.pape()
如何绑定this
我可以访问它吗?
I have a function:
interval(duration, fn) {
var _this = this
this.baseline = undefined
this.run = function(){
if(_this.baseline === undefined){
_this.baseline = new Date().getTime()
}
fn()
var end = new Date().getTime()
_this.baseline += duration
var nextTick = duration - (end - _this.baseline)
if(nextTick<0){
nextTick = 0
}
_this.timer = setTimeout(function(){
_this.run(end)
}, nextTick)
}
this.stop = function(){
clearTimeout(_this.timer)
}
}
Now when I invoke this function as such:
var timer = new this.interval(this.tempo, function(){
this.audio.pause()
this.playBackground()
})
I need to somehow bind 'this' so that it is accessible so that I can call on this.audio.pause()
How do I bind this
so I can access it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论