JavaScript is primarily a language based around objects. Arrays and functions are both objects. Methods are actions that objects can perform. In order to understand how to work with private methods in JavaScript you need to know these fundamentals. Objects are filled with values of data types like strings, numbers and booleans.
When values become a function they are then considered methods. In order to understand private methods, we first have to look at the distinction between a public method.
Public Versus Private Methods
Objects are produced by constructors which initialize the actions inside the function. For a public method, all members inside the object are public. This means that any function can be easily accessed, changed or deleted. One of the first ways of creating a public method is through using this constructor syntax.
function Object(paramType) {
this.value = paramType;
}
Using this constructor we’ve initialized a public method. The following constructor will show the initialization of a private method. We are going to create three different variables inside the private method. These include a paramType, secretValue integer and that. These are all attached inside the object and can’t be manipulated with from the outside. Private methods are internal functions of the overall constructor templates we’ve made. Compare the two of these public and private methods to see the difference.
function Object (paramType) {
this.value = paramType;
var secretValue = 5;
var that = this;
}
We’ll lay out a couple more examples to show how this works then we’ll look at the common pitfalls programmers fall into when working with private methods.
Avoiding Quasi Privacy Methods
Something that might have bothered people for some time with Javascript is the lack of built in private methods. As you can tell in the previous method it is easily possible. But many programmers are uneasy with the fact that some of your methods can be overwritten outside of the function if not properly put into a private function.
One of the prime ways of achieving some type of privacy is through adding a prefix of “_” before members that are supposed to be private. This is an easy fix as it doesn’t actually do anything to the code. It also doesn’t give any private functionality to the methods either, making it a problem. But programmers do it regardless because of the time it saves. Here is how this pseudo-privacy would look like in code, and how you can translate it into a private method.
function Objects (a, b) {
this.a = a;
this.b = b;
this.stuff = [1, 2, 3];
this._initializePrivacy();
}
Taking what we learned from our first example this can be fixed easily to account for real privacy. The following code just changes one line but will allow you to work with a private method.
function Objects (a, b) {
this.a = a;
this.b = b;
this.stuff = [1, 2, 3];
initializePrivacy(this);
}
You now have the ability to apply real privacy to methods without resorting to old tricks that don’t do anything to the code. It’s also good to know that the “_” method isn’t applicable in protecting your private methods from outside interference.