I am trying to pass a method as a callback reference to a method of another class:
function A() {
this.callback = function(){return 0;};
}
A.prototype.set = function(cb) {
this.callback = cb;
};
A.prototype.test = function(){
console.log("test " + this.callback());
};
function B(o) {
this.prop = "blah B";
o.set(this.blah);
}
B.prototype.blah = function() {
console.log(this);
return this.prop;
};
What I would expect from the execution
a = new A();
b = new B(a);
a.test();
is the result
>B { prop="blah B", blah=function()}
>test blah B
But instead the console shows
>A { cb=function(), set=function(), test=function()}
>test undefined
as if the method blah has been assigned to A for the execution..
Why am I getting this result?
And how can I get the result I expected?