I understand that such a question is asked quite often (I probably read each of them in the last few days, trying to figure out how to fix it), but in this case, although Iām sure that I know why this is happening, Iām struggling trying to implement the actual solution.
I am creating a small application using Node.js but having problems creating an object with prototype functions that will not lose their binding when passing them.
Here is what I still have:
foo.js
var Foo = module.exports = function(server) { this.server = server;
bar.js
var Bar = module.exports = function() { this.dataStore =
main.js
var Foo = require('./foo'); var Bar = require('./bar'); var aFoo = new Foo(server); var aBar = new Bar();
As you can probably imagine, passing that the aFoo.sendData function to use as a callback causes it to lose its binding to aFoo, so it cannot find the send function in Foo.
How do I change Foo so sendData supports binding to Foo? Is there a better way to structure this code so that it is not necessary?
source share