BT

InfoQ Homepage News JavaScript Private Class Fields and Methods: Three New Draft Specifications

JavaScript Private Class Fields and Methods: Three New Draft Specifications

Bookmarks

While classes have been available in JavaScript since ES2015, they do not include private fields and methods. These capabilities were scrapped during the initial release due to disagreements within the TC39 committee. Three draft specifications intend to bring these capabilities to JavaScript classes in the near future.

Once these specifications are formally accepted, private fields and methods could be defined using the hash '#' sign. The decision to use the hash sign instead of the more traditional underscore '_' was made to avoid breaking changes in existing libraries, which currently mark private fields that way.

This might sound counter-intuitive, however, despite libraries marking fields or methods as private using the underscore, some developers still reference these fields directly. Turning these into truly private variables could break multiple applications, and therefore slow the adoption of the new specifications.

The problem can be demonstrated by an anecdote from React, a popular JavaScript framework, which had to resort to some rather blunt variable names to deter developers from using them (despite being marked as private) such as '_SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED'.

Each of the three TC39 proposals tackles a different aspect of private fields and methods within classes. However, it should be safe to treat them as a single proposal as they will likely be added to JavaScript at the same time. The proposals are:

Class Field Declaration for JavaScript

It is currently not possible to directly define class fields in JavaScript. Instead, developers define these fields within the constructor.

class Counter { 
  constructor() { 
    this.xValue = 0; 
  } 
}

This proposal adds the ability to define public and private fields directly within the class definition.

class Counter { 
  xValue = 0;
  #yValue = 0;
  constructor() { }
}

Private Methods and Getters/Setters for JavaScript Classes

This proposal tackles the addition of private methods and the use of private getter and setters.

class Counter { 
  get #x() { return #xValue; } 
  set #x(value) { }

  #clicked() { } 
}

Static Class Features

This proposal defines the use of private and public static fields and methods.

class CustomDate { 
  static public() = {} 
  static #private() = {} 
}

It's important to note that overusing private fields and methods can be detrimental to a codebase. Private methods can not be tested using unit-tests and can often be an indication of problems with the Single Responsibility Principle.

Private fields and methods have not officially been accepted into JavaScript, but developers can already use them with Babel. A TypeScript implementation is currently in the works and will be available in a future release.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.