⊗jsOpAdAG 49 of 60 menu

Accessor Getters in OOP in JavaScript

Let's take a closer look at the use of accessor property getters. Let's say we have the following class with a private property:

class User { #name; constructor(name) { this.#name = name; } }

Let's make a public property name in this class that can be read but not written:

class User { #name; constructor(name) { this.#name = name; } get name() { return this.#name; } }

Let's check the work. Let's create an object of our class, passing it the name value as a parameter:

let user = new User('john');

Now let's read the name through the public property:

let name = user.name; console.log(name);

But trying to write down the name will result in an error, just as we wanted:

user.name = 'eric';

Implement accessor getters for the properties of the Employee class.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline