title: How are private fields in javascript implemented in terms of the object model?
body: <p>I'm trying to understand javascript classes in terms of the object model, but the one thing I can't figure out is private fields. Normally, when I want a private field in a class, I do something like:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function A() {
      let privateField = "privateField";
      this.publicField = "publicField";
  
      // Can access privateField
      this.privilegedMethod = function() {
          console.log(privateField);
      }
  
  }
  
  // Cannot access privateField
  A.prototype.publicMethod = function() {
      console.log(this.publicField);
  }
  
  var a = new A();
  a.privilegedMethod(); //"privateField"
  a.publicMethod(); //"publicField"</code></pre>
  </div>
  </div>
  </p>
  <p>Now, privateField is only accessible within A, and to methods written inside of it, and not to the methods written on A's prototype.
  However, private fields in class definitions don't seem to work that way. For instance, if I write:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class A {
      #privateField = "privateField";
      publicField = "publicField";
  
      // Can access privateField
      privilegedMethod = function() {
          console.log(this.#privateField);
      }
      
      // Can somehow also access privateField
      publicMethod() {
          console.log(this.publicField, this.#privateField);
      }
  }
  
  var a = new A();
  a.privilegedMethod(); //"privateField"
  a.publicMethod(); //"publicField privateField"</code></pre>
  </div>
  </div>
  </p>
  <p>I assumed that privateFields would be implemented using closures similarly to the first example, but that doesn't seem to be the case, since publicMethod is a method defined on A.prototype, but it can access privateField. The only explanation I can think of is that privateFields are implemented as a special kind of property on an object that is only visible within classes. This seems to contradict the idea that the class keyword is only syntactic sugar though, since this kind of property can't be assigned without it. I found a link on MDN to the description of class fields on the TC39 github: <a href="https://github.com/tc39/proposal-class-fields" rel="nofollow noreferrer">https://github.com/tc39/proposal-class-fields</a>. It doesn't have many details about private fields, but it seems to suggest that they aren't normal properties, since it states that &quot;private fields cannot be created later, ad-hoc, through assigning to them, the way that normal properties can&quot;. So my question is, is my explanation and its implications correct, and if not, then how are privateFields implemented, and is it possible to create one without the class keyword. Thanks!</p>