Please help me decide whether I should use a function’s prototype object and the “new” keyword or completely stay away from constructor functions.
Situation:
Function called widget() that will be called 10-15 times to initialize each widget on the page. widget() contains quite a few internal methods.
Each time widget() is called, the function needs to return an object that acts as an API to operate on the widget.
Question
1) Do I put all the internal methods inside Widget() under its prototype property? It does not make sense but the main reason for this is to not re-instantiate the internal functions every time widget() is called.
But if I do put the internal functions in prototype, each instantiated w object (w = new Widget();) has access to internal private methods.
2) If I stay away from constructor functions and new keyword and structure my code as down below, how do I fix the performance concern of the internal functions getting re-instantiated every time widget() is called.
function widget()
{
var returnObj = {};
/* Add internal functions but this will be re-instantiated every time */
return returnObj;
}
You have a bit of a tradeoff here. As you seem to already understand, methods you put on the
.prototypeare publicly available, but that is the most efficient places to put methods as they are automatically added to all new copies of that object in a very efficient manner. When using.prototypefor methods, there is only one copy of your methods and a reference to that single copy is automatically added to all new instantiations of that object.But, javascript doesn’t have private methods built-in and the only work-around for that involves not using the
.prototypefor them or for any methods that need to call the private methods.This article by Doug Crockford is a pretty good description of how you can create privacy for either data or methods in any object.
In either case, I don’t see any reason to avoid using the
newkeyword to create new objects. You can make either.prototypeor private methods work withnew.But, if you want to achieve truly private methods, then you can’t use
.prototypefor either the private methods or any methods that need to access them so you have to decide which is more important to you. There is no single correct answer because your need for privacy is situation-specific.In my coding, I generally don’t enforce privacy and I do use
.prototypeandnew. I designate “non-public” methods on the prototype by starting their name with an underscore. This is a notational convention, not an access enforcement scheme.In answer to your second question about avoiding the
newoperator and reinstantiating methods, I’d just ask why you’re doing this? What are you gaining? I’m not aware of any downsides to usingnew. As best I understand your decision about whether to use.prototypevs. manually create/assign methods in your constructor should be about the need for private methods.FYI, 15 objects is hardly going to create a significant difference in performance either way here. I would evaluate your need for true privacy and make your decision based on that. If you HAVE to enforce privacy, then go with the Crockford method for implementing private methods. If you don’t HAVE to have true privacy, then use
.prototype. I don’t see a reason here to avoid usingnewin either case.