Closures implement the class’s private variable approach

Private variables are not shared.

This inside the constructor of the new keyword person is going to point to Tom, open up new space, do it all again,

class Person{  
        constructor(name){ 
            let _num = 100; 

            this.name = name;
            this.getNum = function(){
               return _num;
            } 
            this.addNum = function(){
                return ++_num
            } 
        }
    }
    
    const tom  = new Person('tom')
    const jack  = new Person('jack')
    tom.addNum() 
    console.log(tom.getNum())  / / 101
    console.log(jack.getNum())  / / 100
Copy the code

Private variables can be shared

To avoid the problem of creating a new private variable for each power, we can put the private variable outside the class constructor and continue to return the variable through the closure.

const Person = (function () {
        let _num = 100;

        return class _Person {
            constructor(name) {
                this.name = name; 
            }
            addNum() {
              return ++_num
            }
            getNum() {
              return _num
            } 
        }
    })() 

    const tom = new Person('tom')
    const jack = new Person('jack') 
    tom.addNum()
    console.log(tom.getNum())  / / 101
    console.log(jack.getNum())  / / 101
Copy the code

That way, if you mix the two methods, you can have both shareable and unshareable private variables.

Disadvantages: Instantiation will increase a lot of copies, more memory consumption.

Symbol implements the class’s private variable approach

Symbol:

Create a unique value where all symbols are not equal. Add the description Symble(“desc”) to the value.

const name = Symbol('name')
const person = { / / the name of the class
    [name]:'www'.say(){
        console.log(`name is The ${this[name]} `) 
    } 
}  
person.say()
console.log(name)
Copy the code

Keys created for objects using Symbol cannot be iterated and Json serialized, so their primary purpose is to add a unique value to the object. But you can use getOwnProporitySymbols() to get the Symbol.

Cons: New syntax browser compatibility is not very wide.

Symbol implements the private variable of the class

We recommend using a closure to create a reference to Symbol, so that the reference can be obtained in the class’s method section. This avoids the memory waste caused by writing the method in the constructor and reinventing the spatial assignment method every time a new instance is created.

const Person = (function () {
  let _num = Symbol('_num: private variable ');
  
  return class _Person {
    constructor(name) {
      this.name = name;
      this[_num] = 100
    }
    addNum() {
      return ++this[_num]
    }
    getNum() {
      return this[_num]
    } 
  }
})()

const tom = new Person('tom')
const jack = new Person('jack')

console.log(tom.addNum()) / / 101
console.log(jack.getNum()) / / 100
Copy the code

Create private variables with WeakMap

MDN profile

Implementation:

const Parent = (function () {
  const privates = new WeakMap(a);return class Parent {
    constructor() {
      const me = {
        data: "Private data goes here"
      };
      privates.set(this, me);
    }
    getP() {
      const me = privates.get(this);
      return me
    }
  } 
})()

let p = new Parent()
console.log(p)
console.log(p.getP())
Copy the code

conclusion

To sum up, the above weakMap method is the most recommended implementation method to realize the saving of similar private variables, which is easy to recycle and compatible with more browsers.