Using call
here does not help: it calls setTimeout
with your this
object, but the callback function itself is still called from the global scope. What you really want to do is something like this:
function foo() { var self = this; this.bar = function() { console.log("keep going"); window.setTimeout(function() { self.bar(); }, 100); } this.bar(); }
Edit: If you really want something similar to the call
approach, you can use bind
which binds the this
value to the function:
window.setTimeout(this.bar.bind(this), 100);
However, this is part of the new ECMAScript 5 specification, which is not yet supported by all browsers.
casablanca
source share