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!


Abhang Rane


16 comments :

Reader's Comments

  1. 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.

    ReplyDelete
  2. dank vapes are the best brand of vape cartridges in the market. These dank vapes cartridges have however suffered a backlash in the recent weeks with multiple reports on how these dank vapes carts cause cancer and other related lung disease.
    dank vapes cart however remain one of the best cartridges. dank vapes are sold in multiple states over the USA. dank vapes cart compost one of the black markets finest vape products. But this backlash has caused a fall in the consumption of the vape by adults.The 710 Kingpen brand is also on the rise in market demands as well as skywalker OG products.
    Recently juul pods also faced a similar backlash. juul starter kits have also fallen in market demand. mango juul pods are refillable juul pods and can be gotten as juul pods bulk. juul skins and juul accessories all come with the juul kits.
    buy juul online at cheap prices buy cocaine online
    buy Keytruda online and other cancer medications such as buy avastin online , buy Herceptin online
    jungle boys , buy weed online

    ReplyDelete
  3. Thanks for this.

    buy weed online at Legit Weed Store

    order your cannabis now at Order Cannabis Meds

    Buy marijuana online at 420 Delivery Store

    Buy medical cannabis online at at Canna Med Shop

    Get your Pill refills at Pill Pharma

    Köp piller online i Sverige kl Mundi Apotek

    Köp Oxycontin Online Billigt på Scandinavia Apotek

    Billiga piller och steroider till salu på Battrehalsa Apotek


    ReplyDelete
  4. Thanks for this. I really appreciate your good work

    content so valid. keep up i love this

    so much meaning. I enjoyed my self


    I cant neglect. Reading such a content

    Ethical facts. one just cant neglect

    I always appreciate. Valid content

    Never a dull. moment reading online

    I have learned. A new thing today

    nothing is so meaningful. like reading a good content

    ReplyDelete
  5. Thanks for this. I really appreciate your good work

    content so valid. keep up i love this

    so much meaning. I enjoyed my self


    I cant neglect. Reading such a content

    Ethical facts. one just cant neglect

    I always appreciate. Valid content

    Never a dull. moment reading online

    I have learned. A new thing today

    nothing is so meaningful. like reading a good content

    ReplyDelete
  6. https://k2incenseonlineheadshop.com/
    info@k2incenseonlineheadshop.com
    k2incenseonlineheadshop Buy liquid incense cheap Buy liquid incense cheap For Sale At The Best Incense Online Shop

    ReplyDelete
  7. Nice knowledge gaining article. This post is really the best on this valuable topic.
    promethazine cough syrup

    ReplyDelete