Saving private variable

TL;DR – This post is about implementing private variables in Javascript. I could have named this post accordingly but what’s the fun in that!

Implementing something as simple as private variables is a topic of discussions across the Javascript community. The reason is the “open” nature of Javascript wherein those typical OOPS fundamentals of encapsulation, information hiding are not natively taken into consideration. Recent converts of Javascript (like me) somehow cannot shed off the protective cover of OOPS fundamentals and hence try to twist and mould a language to fit into those well learnt dies. Javascript purists seem to disagree with such approach and embrace the language and change the programming style with it. I do agree with it, but I think there are times when there are valid reasons for classical style of inheritance, private variables, abstractions in a language which does not have a concept of class. One such case is to have private variables in Javascript. It is such a common requirement to hide those sensitive little secrets of a “class” from the ruthless outside world. There are several ways to achieve this in Javascript and you can just find them easily on the internet. Here is a way I have been recently implementing private variables using WeakMap (part of ES6 standard). I always feel the need for a function similar to GetHashCode() that we have readily available in .NET. Sometimes when “this” pointers fucks us and I have no clue which instance I am looking at, a unique id given to each instance of a “class” can be life saver. But we do not want people to change that hash value for an instance once assigned so lets keep it private.

var globalObj = globalObj || {};
(function () {
'use strict';

var privateParts = new WeakMap();

globalObj.MyObject = function(id){

this.publicStuff = 'You can see me.'; //public

var privatePart = (function(instance){

var obj = instance;
var hashCode = Math.random(); // private

function getHashCode(){
return hashCode;
}

return{
getHashCode : getHashCode
};

}(this));

privateParts.set(this, privatePart); // This would bind 'this' object with the privatePart.

globalObj.MyObject.prototype.getHashCode = function(){

return privateParts.get(this).getHashCode();

};
};
})();

var obj1 = new globalObj.MyObject('MyObject1');
var obj2 = new globalObj.MyObject('MyObject2');
var h1 = obj1.getHashCode();
var h2 = obj2.getHashCode();
console.log(h1);
console.log(h2);

Happy Programming!


3 Comments:

diljeet kaur said...

Thank you for giving so good information.You can find chandigarh escorts for good escorts services in chandigarh.
chandigarh escorts ,
chandigarh call girls ,
call girls in chandigarh ,
escorts in chandigarh ,
escorts services in chandigarh ,
female escorts in Chandigarh ,
independent escorts in chandigarh

Shilpa Escorts said...

We continually go for conceptualizing to raise the status of our young lady's execution. escort service in dehradun We design a different sort of exercises with the assistance of sex help segments so our escort discovers brilliance over the pleasurable demonstrations.

rashmi sharma said...

About the off likelihood that you simply hope to find two females for an incall then simply a single of these needs a spot wherever she could engage you. The other woman will go to the space.
The booking would be an out call for her. When needing an out call booking, then please let's know if it will be at a lodging or your own own particular place. In the event it is in an inn, we definitely necessitate the inn identify, and exactly what time the reservation will begin.
Escort Sevice in Jaipur

Post a Comment