[JavaScript] How to use private variables when creating a class with the Class keyword

Recently encountered this problem and checked a lot of information

When creating classes in previous versions, we can easily use private variables and methods.
For example:

function Constructor(param){
    
    
    var that = this;
    var private = //..
    this.public = //..
    
    this.method = function () {
    
    
        console.log(private);
        console.log(this.public);
    }
}

Because the entire constructor is in a scope, we can access the private members of the outer scope in any method.

However, when using the class keyword to create a class, the constructor and method are separated, and it is not grammatical to write code directly in the class, so it is difficult to use private members.
In other words, the direct external scope of the constructors and methods in the class is the direct external scope of the class. If it is in the global context, it is the global scope. As a class, can't let him rely on the external scope, right?

In the process of checking information, many people are using this method

I just posted two pieces:
Insert picture description here
Insert picture description here
The idea is to wrap a layer of function outside the class, and then pass the class as a closure, so that the direct external scope accessed by the class is closed.

I don't understand why so many people are talking about this method. It has a big flaw: the external scope of class is unique, that is, a set of private variables will be shared between multiple instances.

But I am always not confident enough in myself, thinking it is some magic mechanism, and I do not understand these mechanisms.
So I tried it and found that there would indeed be conflicts between multiple instances. .

I thought about it, the only feasible way of this idea is to re-create an external scope
code every time an object is instantiated :

 function Constructor(param) {
    
    
     var that = this;
     var privateVar = //..
     var privateFun = function () {
    
    
         console.log(that);
     }
     return new class {
    
    
         constructor() {
    
    
             this.public = param;
         }

         method(){
    
    
             console.log(private);
             console.log(this.public);
         }
     }();
 }

Although this can solve the problem, the complexity of the construction is much higher, and every time an object is instantiated, it is actually instantiated through a new class, and there is always a feeling of taking a big advantage.

There is no good way yet. If it is solved, I will come back and update. If anyone knows how to do this, please feel free to enlighten me.


Ashamed, I just learned that the concept of private members already exists in the new standard, and mainstream browsers have basically implemented it

class {
    
    
	#private = //..
	constructor() {
    
    
	     this.public = param;
	 }
	 method(){
    
    
	     console.log(this.#private);
	     console.log(this.public);
	 }
}

Guess you like

Origin blog.csdn.net/qq_16181837/article/details/105363553