I have following Javascript objects:
function Alfa() {
this.status='';
this.setStatus = function(value) {
this.status=value
}
}
function Beta() {
this.setSomething = function(setter) {
setter('Something');
}
}
And than execution:
alf=new Alfa();
alf.setStatus('foo');
This works fine, if you look into values of alf, the status is set to 'foo', but:
bet=new Beta();
bet.setSomething(a.setStatus);
What I have expected is that bet will execute setStatus function of alf and set status value to 'something', however, this is not happening. setStatus of alf is executed, but this points not to alf instance but to Window, so the property status is set for Window not for alf.
What is the proper approach to get desired functionality?
This is of course simplified implementation, in real life i want to pass those functions of alf to event handlers, so I cannot change Beta implementation, eg:
$('xxx').click(alf.setStatus)