I was wondering which of the following would be more efficient in a Node API
function LoginController() {
this.model= new Model();
};
LoginController.prototype.doSomething = function() {
this.model.doSomethingToo();
}
versus this:
function LoginController() {
};
LoginController.prototype.doSomething = function() {
new Model().doSomethingToo();
}
As far as I understand prototypal objects in scenario #1 I would create a new Model every time I call new LoginController().
In scenario #2 I would create a new Model only once when creating the first new LoginController(). All next new instances would not create an other Model, because it was already created in the prototypal function.
Is that correct?
Modelevery timeloginControllerInstance.doSomethingis executed.doSomething, is defined only once with theprototype, but its contents are still reevaluated with each call. With #2, every use oflogin.doSomething()would create anew Model().