Sitemap
Gourav More

Gourav More

Private Members in JavaScript

3 min readAug 23, 2020

--

As we all know — JavaScript is the world’s most misunderstood programming language. Some of us believes that — objects cannot have private instance variables and methods. But this is a misunderstanding. JavaScript objects can have private members.

Objects

JavaScript is fundamentally all about objects. Arrays are objects. Functions are objects. Objects are objects. So what are objects???

Objects are collections of name-value pairs. The names are strings, and the values are strings, numbers, booleans, and objects (including arrays and functions). Objects are usually implemented as hashtables so values can be retrieved quickly.

Hashtables — is a data structure where the data stored in an array format, where each data value has its own unique index value. If we know the index of the data, we can access the data very fast.

so, this is a hashtables, moving back to the objects —

If a value is a function, we can consider it a method. When a method of an object is invoked, the this variable is set to the object. The method can then access the instance variables through the this variable.

Objects can be produced by constructors, which are functions which initialize objects. Constructors provide the features that classes provide in other languages, including static variables and methods.

Public

The members of an object — all are public members. Any function has access to that public members. They can add, delete or modify those members.

there are two way to add members into the new object

  • constructor
  • prototype

In the Constructor : This technique is used for to initialize public instance members. The constructor has a this variable. We can add the members using the constructor’s this variable.

function Container(param) {
this.member = param;
}

if we construct new object => var myContainer = new Container(‘abc’);

then => myContainer.member contains ‘abc’.

In the prototype: By using this technique we can add a public members to the object. When the member of the object is sought and not found in a object then its taken from the constructor prototype member. This technique is used for the inheritance and it also conserves memory.

Container.prototype.f1 = function (string) {
return this.member + string;
}

we can invoke the method => myContainer.stamp(‘def’)

Get Gourav More’s stories in your inbox

Join Medium for free to get updates from this writer.

which will produce the output => “abcdef”

For more… you can check my another story on: Javascript prototype https://medium.com/@gourav_m/javascript-prototypes-e61a9953447a

Private

The private members are made by the constructor. We can not access this outside the object

function Container(param) {
this.member = param;
var privateMember1 = 3;
var privateMember2 = this;
}

In this above constructor we can see the three private instance members

  • param
  • privateMember1
  • privateMember2

All these members are attached to the object and we can access this members inside the objects private methods, but we can not access this from outside of the object nor its accessible to the objects own public methods

function Container(param) {

function privateFunction() {
if (privateMember1 > 0) {
privateMember1 -= 1;
return true;
} else {
return false;
}
}

this.member = param;
var privateMember1 = 3;
var privateMember2 = this;
}

If we do => var P1 = new Container(“abc”);

the P1 will contains =>

This means you can access only the member variable of the object and you can not access param, privateFunction, privateMember1 and privateMember2 because they are private members. But you can access the private members into the private functions.

Patterns:

Public:

function Constructor(…) {
this.membername = value;
}
Constructor.prototype.membername = value;

Private:

function Constructor(...) {
var that = this;
var membername = value;
function membername(...) {...}
}

--

--