FrontendDeveloper.in

ECMAScript question detail

Private Class Variables

In ES6, the classes are introduced to create reusable modules and variables are declared in clousure to make them private. Where as in ES2020, private class variables are introduced to allow the variables used in the class only. By just adding a simple hash symbol in front of our variable or function, you can reserve them entirely for internal to the class.

class User {
#message = "Welcome to ES2020"

login() { console.log(this.#message) }
}

const user = new User()

user.login() // Welcome to ES2020
console.log(user.#message) // Uncaught SyntaxError: Private field '#

Note: As shown in the above code, If you still try to access the variable directly from the object then you will receive syntax error.

ES2020 Or ES11

ES2020 is the current newer version of ECMAScript corresponding to the year 2020. This is the eleventh edition of the ECMAScript Language Specification. Even though this release doesn't bring as many features as ES6, it included some really useful features.

Most of these features already supported by some browsers and try out with babel parser support for unsupported features. This edition is set for final approval by the ECMA general assembly in June, 2020. The ECMAScript 2020 (ES2020) language specification is ready now.

Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399