I am trying to create a class that in its constructor uses some helper functions. Is there a way to move these helpers to the prototype? The problem is that the constructor does some asynchronous calls to the database and I need to apps in a callback function so I can continue execution after the data was retrieved.
I want to move stuff to the prototype, because if I understood correctly, these functions are not tied to a single object, so if I have multiple objects they will still call the same code but with different context.
Class = function(id, callback) {
var that = this,
addCore = function(err, model) {
that.id = model._id
that.core = model
callback(err, that)
},
addTopology = function() {
}
if (arguments.length === 2) {
gameOps.findById(id, addCore)
} else {
callback = id
gameOps.create(addCore)
}
}
Class.prototype = {
addPlayer: function(player, callback) {
gameOps.addPlayer(player, this.model, callback)
}
}