Node implements ECMAScript 5, which has Function.bind() .
I think this 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));
This works, however, when using a closure as a callback, like you, the most common way is to save the context in a variable that can be used in closure.
This method is more common, because many times callbacks can be deep, and bind for 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) {
source share