Ways to pass/keep context:
-
Anonymous callback, e.g.
var context = { callback: function(){ // ... } }; asyncThing(function(){ // if it's a property, context.callback(); // if not, callback.apply(context); });
-
Pass the context and the function as parameters, e.g.
asyncThing(callback, context);
-
Using
bind()
, e.g.// homegrown var boundCallback = bind(callback, context); // or built-in (to newer browsers) var boundCallback = callback.bind(context); asyncThing(boundCallback);