I am dealing with JavaScript objects as outlined below:
function Alfa() {
this.status='';
this.setStatus = function(value) {
this.status=value
}
}
function Beta() {
this.setSomething = function(setter) {
setter('Something');
}
}
When I execute the following:
alf=new Alfa();
alf.setStatus('foo');
It works perfectly fine, and upon inspecting the values of alf, the status is indeed set to 'foo'. However, when I try the following:
bet=new Beta();
bet.setSomething(a.setStatus);
I expected bet to execute the setStatus function of alf and set the status value to 'something', but that's not the case. The setStatus of alf gets executed, but the context points to Window instead of alf, resulting in the status property being set for Window rather than alf.
What would be the correct approach to achieve the desired functionality?
This example is simplified; in reality, I intend to pass these functions of alf to event handlers without altering the implementation of Beta, such as:
$('xxx').click(alf.setStatus)