-1

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)
  }
}
0

1 Answer 1

1

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.

Yes. However, that is not what you want: The asynchronous callbacks need to be tied on specific instances.

If you don't want to have too much stuff floating around your constructor, you might reconsider your design:

function Class(model) {
    this.id = model._id;
    this.core = model;
}
Class.prototype.addPlayer = function(player, callback) {
    gameOps.addPlayer(player, this.model, callback);
};
Class.fromDatabase = function(id, callback) {
    function addCore(err, model) {
        if (err)
            callback(err);
        else
            callback(null, new Class(model))
    }
    function addTopology() {
    }

    if (arguments.length === 2) {
        gameOps.findById(id, addCore)
    } else {
        callback = id
        gameOps.create(addCore)
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

It is nice, but now how do I pass a callback to the constructor?
OK, I see it, just it's a bit tricky. So I create a class by calling Class.fromDatabase and I get my object back as the second argument of the callback?
Yes, exactly. The instance is not created instantly, but only available to the callback. It's cleaner since you have no uninitialised instances floating around, and advocates better async programming. It also translates nicely into the Promise abstraction.
Also, will the new Class(model) return before the callback is called? I mean the simple calls in js are executed in sequence, right?
Yes, it will. The newly created instance will be passed as an argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.