There is no such thing as a "source context" function. You would need to do something like this:
subscribe: function(handler, context) { if (typeof(handler) === 'function') { handlers.push([handler, context]); } },
And then of course
handlers[handler][0].apply(handlers[handler][1], args);
Alternatively (this is what I would do), leave it to the caller to make sure the handler has the correct context. For example, instead of delegate.subscribe(this.foo)
, let's say
var self = this delegate.subscribe(function () { self.foo() })
Or using Function.prototype.bind ,
delegate.subscribe(this.foo.bind(this))
source share