As compared to other languages there is no Private keyword that can be used to declare private variables when creating object and classes in JS. Thats where closures
come in to help. We use the property of closures to keep the scope of variables closed inside the function. In JS its a general rule to put an underscore in front of private variable.
Here’s an example of Stopwatch implemented using factory function method:
The scope of _duration
in the above function is only available to the returning object. If we try to access it with s1._duration
it returns undefined
which is exactly what we expect from a private variable. We are accessing it via the function getDuration()
.
In ES6 does not have a proper way to create private as its still just a syntactic sugar around the old methods of creating objects. So we can make use of IIFE
(Immediately Invoked Function Expression) way to create a closure and keep the private variables. If we try to access the property duration it will be undefined but it can be accessed using our method getDuration()
.
Here’s an ES6 implementation:
Another way of doing is using Weakmap
outside the class and use it to keep the private variable which is garbage collected after the function finishes executing.