Page 180 - JavaScript
P. 180

this.enqueue = function (type) {  // - privileged public method
                                               //   accessing the local state
               list.push(type);                //   "writing" alike.
               return type;
             };
             this.dequeue = function () {      // - privileged public method
                                               //   accessing the local state
               return list.shift();            //   "reading / writing" alike.
             };
           }
         }


         var q = new Queue;            //
                                       //
         q.enqueue(9);                 // ... first in ...
         q.enqueue(8);                 //
         q.enqueue(7);                 //
                                       //
         console.log(q.dequeue());     // 9 ... first out.
         console.log(q.dequeue());     // 8
         console.log(q.dequeue());     // 7
         console.log(q);               // {}
         console.log(Object.keys(q));  // ["enqueue","dequeue"]


        With every instantiation of a Queue type the constructor generates a closure.

        Thus both of a Queue type's own methods enqueue and dequeue (see Object.keys(q)) still do have
        access to list that continues to live in its enclosing scope that, at construction time, has been
        preserved.


        Making use of this pattern - emulating private members via privileged public methods - one should
        keep in mind that, with every instance, additional memory will be consumed for every own property
        method (for it is code that can't be shared/reused). The same is true for the amount/size of state
        that is going to be stored within such a closure.


        Dynamic Method Names


        There is also the ability to evaluate expressions when naming methods similar to how you can
        access an objects' properties with []. This can be useful for having dynamic property names,
        however is often used in conjunction with Symbols.


         let METADATA = Symbol('metadata');

         class Car {
           constructor(make, model) {
             this.make = make;
             this.model = model;
           }

           // example using symbols
           [METADATA]() {
             return {
               make: this.make,
               model: this.model
             };



        https://riptutorial.com/                                                                             137
   175   176   177   178   179   180   181   182   183   184   185