I have an object created with a constructor:
var mngr = new Manager();
It has a property contacts
and a method showContacts() { return this.contacts }
.
I also have another object, say, named company
which runs some actions then in the end it needs to make mngr
object run its showContacts()
I declare company and add a method from mngr to it:
var company = new Company({
onFinish: mngr.showContacts
});
Now when I run the code from company object, it executes the function onFinish
which is borrowed from mngr.showContacts
, so it runs in context of the company, not mngr.
How do I make it perform the method on the mngr?
I have these variants:
var company = new Company({
onFinish: mngr.showContacts.bind(mngr)
});
and:
var company = new Company({
onFinish: function(){mngr.showContacts()}
});
Both seems not perfect.
How should I do it the right way?