ONLINE TUTORIALS

A Coding Blog by Irshad

  1. Home
  2. Articles
  3. Private Class Attributes and Methods in JavaScript Three New Specs.

Private Class Attributes and Methods in JavaScript Three New Specs.

While classes are available in JavaScript since ES2015, they do not offer private attributes and methods. These possibilities were ruled out at the time of the initial release, due to disagreements within the TC39 committee. These draft specifications are intended to provide these capabilities to JavaScript classes shortly.

 

Once these specifications are formally accepted, private attributes and methods can be defined using the pound sign ‘# “. The decision to use the pound sign rather than the more traditional underscore’ _ ‘was made to avoid” breaking changes “in existing libraries that indicate private attributes through this.

 

This might seem counterintuitive, yet despite the existence of libraries indicating attributes or methods as private with an underscore, developers continue to use them.

 

The problem can be demonstrated with an anecdote from React, a popular JavaScript framework, which had to use variable names powerful enough to deter developers from using them (although they are marked as private), such as’ _

 

SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED ‘.

Each of the 3 TC39 committee proposals addresses a different aspect of private attributes and methods in classes. However, it should be safe to treat them as a single proposition as they will likely be added to JavaScript at the same time. The proposals are:

CLASS ATTRIBUTION STATEMENTS IN JAVASCRIPT

 

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

class Counter {

constructor() {

this.xValue = 0;

}

}

This proposal adds the possibility of defining public and private attributes directly in the definition of the class.

class Counter {

xValue = 0;

#yValue = 0;

constructor() { }

}

METHODS AND PRIVATE ACCESSORS FOR JAVASCRIPT CLASSES

 

This proposal addresses the addition of private methods and the use of private accessors.

class Counter {

get #x() { return #xValue; }

set #x(value) { }

 

#clicked() { }

}

STATIC CLASS MEMBERS

 

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

class CustomDate {

static public() = {}

static #private() = {}

}

It is important to note that the heavy use of private attributes and methods can damage a codebase. Private methods cannot be tested on a unitary basis and can often be an indication of problems with the Single Responsibility Principle.

Private attributes and methods have not yet been officially integrated into JavaScript, but developers can use them with Babel now. A TypeScript implementation is currently in the works and will be available in a later release.

Search Posts

Let’s Connect

Instagram
YouTube

Category

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu