Prototypical function bar
is executed elsewhere, in a Node.js environment (where bind
should be available). I want this
inside bar()
function to be the instance of my object:
var Foo = function (arg) {
this.arg = arg;
Foo.prototype.bar.bind(this);
};
Foo.prototype.bar = function () {
console.log(this); // Not my object!
console.log(this.arg); // ... thus this is undefined
}
var foo = new Foo();
module.execute('action', foo.bar); // foo.bar is the callback
... why bar()
logs undefined
and this
is not my instance? Why the execution context was not changed by the bind
call?