javascript - scope of `this` changing on a production server? -
i have following 3 functions in backbone view. originally, call updatetimer
within playback
, startrecording
functions triggered error, went away after preceded call this
, in this.updatetimer
. however, i've put demo app on production server (where code compiled 1 file) , call this.updatetimer triggering error
uncaught typeerror: object #<object> has no method 'updatetimer' (repeated 28 times)
can explain why might be?
the 3 functions working on local machine:
playback: function(e){ e.preventdefault(); this.updatetimer(0); sc.recordplay({ progress: function(ms) { this.updatetimer(ms); } }); }, updatetimer: function(ms){ $('.status').text(sc.helper.millisecondstohms(ms)); }, startrecording: function(e){ $('#startrecording').hide(); $('#stoprecording').show(); e.preventdefault(); sc.record({ progress: function(ms, avgpeak) { this.updatetimer(ms); } }); },
from console of production server
,playback: function(t) { t.preventdefault(), this.updatetimer(0), sc.recordplay({progress: function(t) { this.updatetimer(t) }}) },updatetimer: function(t) { $(".status").text(sc.helper.millisecondstohms(t)) },startrecording: function(t) { $("#startrecording").hide(), $("#stoprecording").show(), t.preventdefault(), sc.record({progress: function(t) { this.updatetimer(t) uncaught typeerror: object #<object> has no method 'updatetimer' (repeated 28 times) }})
you getting error because this
context lost in inner function passing sc.record
, no longer refers backbone view. there few ways around this, 1 of take advantage of closures , create reference correct this
, use in progress
callback:
startrecording: function(e){ var self = this; $('#startrecording').hide(); $('#stoprecording').show(); e.preventdefault(); sc.record({ progress: function(ms, avgpeak) { self.updatetimer(ms); } }); },
same thing goes call updatetimer
in call sc.recordplay
in playback
.
Comments
Post a Comment