The scoping function offers the only privacy in JavaScript.
So canonical:
function Ctor(dep1, dep2) { this._dep1 = dep1; this._dep2 = dep2; } Ctor.prototype.foo = function() {
... is problematic in that it does not offer encapsulation for nested dependencies.
An alternative (albeit slightly different from the location of foo ) that offers real encapsulation might be:
function factory(dep1, dep2) { return { foo: partial(foo, dep1, dep2),
But I rarely see this pattern. Is there a good reason not to use the latter?
source share