Page 182 - JavaScript
P. 182

As such, they won't be revealed using for var in or Object.keys.


        Thus we can use symbols to store private data.


         const topSecret = Symbol('topSecret'); // our private key; will only be accessible on the
         scope of the module file
         export class SecretAgent{
             constructor(secret){
                 this[topSecret] = secret; // we have access to the symbol key (closure)
                 this.coverStory = 'just a simple gardner';
                 this.doMission = () => {
                     figureWhatToDo(topSecret[topSecret]); // we have access to topSecret
                 };
             }
         }


        Because symbols are unique, we must have reference to the original symbol to access the private
        property.


         import {SecretAgent} from 'SecretAgent.js'
         const agent = new SecretAgent('steal all the ice cream');
         // ok lets try to get the secret out of him!
         Object.keys(agent); // ['coverStory'] only cover story is public, our secret is kept.
         agent[Symbol('topSecret')]; // undefined, as we said, symbols are always unique, so only the
         original symbol will help us to get the data.


        But it's not 100% private; let's break that agent down! We can use the
        Object.getOwnPropertySymbols method to get the object symbols.


         const secretKeys = Object.getOwnPropertySymbols(agent);
         agent[secretKeys[0]] // 'steal all the ice cream' , we got the secret.


        Using WeakMaps



        WeakMap is a new type of object that have been added for es6.


        As defined on MDN

              The WeakMap object is a collection of key/value pairs in which the keys are weakly
              referenced. The keys must be objects and the values can be arbitrary values.


        Another important feature of WeakMap is, as defined on MDN.

              The key in a WeakMap is held weakly. What this means is that, if there are no other
              strong references to the key, the entire entry will be removed from the WeakMap by the
              garbage collector.


        The idea is to use the WeakMap, as a static map for the whole class, to hold each instance as key
        and keep the private data as a value for that instance key.

        Thus only inside the class will we have access to the WeakMap collection.




        https://riptutorial.com/                                                                             139
   177   178   179   180   181   182   183   184   185   186   187