Private field must be declared in an enclosing class in JS

avatar
Borislav Hadzhiev

2 min

banner

# Private field must be declared in an enclosing class

The "Private field must be declared in an enclosing class" error occurs when we try to read a private value directly from our code outside of the class object.

To solve the error, make sure to only read private values from inside of the class.

private field must be declared in enclosing class

Here's an example of how the error occurs.

index.js
class Person { #num = 100; } const p = new Person(); // โ›”๏ธ Private field '#num' must be declared in an enclosing class console.log(p.#num);

We got an error because we tried to access a private value directly in our code, outside of the class.

Private fields (fields prefixed with #) are accessible on the class constructor from inside the class declaration.

Make sure you don't try to set private fields inside of the constructor() function of your class without having initialized them in the body of the class.

# Only access private fields from inside the class

Make sure to only access private fields from inside the class.

index.js
class Person { // ๐Ÿ‘‡๏ธ Private field #num = 200; // ๐Ÿ‘‡๏ธ Another private field #another_private; constructor(first, last) { this.first = first; this.last = last; this.#another_private = 'another private field'; } getAnother() { return this.#another_private; } getNum() { // ๐Ÿ‘‡๏ธ Accessing private field return this.#num; } getName() { // ๐Ÿ‘‡๏ธ Calling private method return this.#fullName(); } // ๐Ÿ‘‡๏ธ Private Method #fullName() { return `${this.first} ${this.last}`; } } const p = new Person('Bobby', 'Hadz'); console.log(p.getName()); // ๐Ÿ‘‰๏ธ "Bobby Hadz" console.log(p.getNum()); // ๐Ÿ‘‰๏ธ 200 console.log(p.getAnother()); // ๐Ÿ‘‰๏ธ another private field

We declared a private field #num and a private method #fullName.

Make sure to declare any private fields before accessing them.

Notice that we initialized the another_private field in the body of the class before setting it in the class's constructor method.

We can access the private fields and methods from inside the scope (inside of the class), but if we try to access them directly from our code, we would get the error.

To get around this you have to implement methods in the class that interact with the private fields and methods and you have to expose these methods.

# Initializing a private field without a value

You can also initialize a private field without a value and set its value at a later point.

index.js
class Person { // ๐Ÿ‘‡๏ธ Private field #num; increment() { this.#num = typeof this.#num === 'number' ? this.#num + 100 : 100; return this.#num; } } const p = new Person(); console.log(p.increment()); // ๐Ÿ‘‰๏ธ 100 console.log(p.increment()); // ๐Ÿ‘‰๏ธ 200 console.log(p.increment()); // ๐Ÿ‘‰๏ธ 300

We declared a private field #num without initializing its value. Note that a private field can only be defined once in a single class.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2023 Borislav Hadzhiev