node js pass context to callback
node.js
Solution
Node implements ECMAScript 5, which has `Function.bind()`.
I think that is what you are looking for.
exports.Handlers.prototype.getItemById = function(id) {
var item = this.db.getItemById(id, (function(error, item) {
item.name = this.prefix + item.name;
...
...
}).bind(this)); //bind context to function
};
This works, however when using a closure as a callback, like you are doing, the most common way this is done is by storing the context in a variable that can be used in the closure.
This method is more common because many times callbacks can be deep and calling `bind` on each callback can be heavy; whereas defining `self` once is easy:
SomeObject.prototype.method = function(id, cb) {
var self = this;
this.getSomething(id, function(something) {
//self is still in scope
self.getSomethingElse(something.id, function(selse) {
//self is still in scope and I didn't need to bind twice
self.gotThemAll(selse.id, cb);
});
});
};
Problem
I am using node.js. I have this handlers.js file: ``` exports.Handlers = function(prefix) { this.prefix = prefix; this.db = new DBInstance(); }; exports.Handlers.prototype.getItemById = function(id) { var item = this.db.getItemById(id, function(error, item) { item.name = this.prefix + item.name; ... ... }); }; ``` When I call: ``` var h = new Handlers(); h.getItemById(5); ``` I get an error since the context isn't the Handlers and this.prefix doesn't exists. I can fix it using this: ``` exports.Handlers.prototype.getItemById = function(id) { var scope = this; var item = this.db.getItemById(id, function(error, item) { item.name = scope.prefix + item.name; ... ... }); }; ``` Is there any better way to pass the context to the callback? What is the nodejs common way to pass the context to the callback?