Reputation: 4771
I am trying to make sense and understand when to use private and when to use privileged methods in JavaScript. According to this post: Private Members in JavaScript, private members of an object can be accessed and modified both by private methods and privileged methods. When writing getter methods (for the private members), would you use a private or privileged method? Why? For accessors (methods that modify private members) would you use private or privileged methods? Why? What is the standard technique used in this cases?
And lastly, what is the difference in using a private vs privileged method to access a private variable (apart from the fact that the private method cannot be called from outside).
function ClassB(given)
{
var myVar = given;
function _get ()
{
return myVar;
};
this.get = function ()
{
myVar = myVar + 3;
return myVar;
};
}
Upvotes: 3
Views: 4532
Reputation: 11369
When you are designing your objects you need to make a decision to start. Do you care about exposing the internals of the object to the outside world?
If you don't care, then you should really be adding all of your methods to the prototype of the object, and all your instance variable to this
in your constructor function. This approach may not be desirable in some situations. This is where the privileged methods come in, as well as the usage of closures to mimic private methods.
When you create a privileged method, it has access to private and public members of the object as you've noted. The big difference between creating a privileged method and a public method on the prototype is that the privileged method is created with every instance of the object, whereas the public method on the prototype is only created once. If you have many privileged methods and you are creating A LOT of instances of your objects, you could see how this may not be desirable when it comes to memory usage.
I assume you are looking to hide some of your internal functionality from the outside world, so you will need to make use of privileged methods. For all your getters/setters, you will need to define them as privileged and add them to this
(otherwise there isn't much point in creating getter/setters if you plan on leaving them private):
function SomeClass() {
// Private member
var _privateMember = 1;
// Public variable
this.publicMember = 1;
// Privileged getter method
this.getPrivateMember = function() {
return _privateMember;
};
// Privileged setter method
this.setPrivateMember = function(newVal) {
_privateMember = newVal;
};
// Private method that has access to '_privateMember' and 'this'
var _privateMethod = function() {
// Modify private member
_privateMember = 2;
// Modify public member
this.publicMember = 1;
// Do some other fancy processing privately
};
this.doSomethingAwesome = function() {
// Do some stuff here...
// Call your private method maybe?
_privateMethod();
};
}
// Public methods on prototype do not have access to private methods in your constructor
SomeClass.prototype.publicMethod = function() {
// Cannot access `_privateMember`
this.publicMember = 2; // Does have access to 'this'
};
As for your last question, about using a private vs. privileged method for modifying some private members... As you've said, you wouldn't be able to access it from the outside world which means it would be pretty useless unless you are adding other functionality to your private getter method, and exposing it to the outside world via a privileged method. If you do a lot of heavy lifting inside the constructor of your object, this could be a valid reason to create a reusable getter/setter method, but that really depends on your use case.
If this did not answer what your question was, or if you would like any other specific explanation about something feel free to ask.
Upvotes: 4