⊗jsOpAdAS 50 of 60 menu

Accessor Setters in OOP in JavaScript

Now let's make an accessor setter in addition to the getter:

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

Now let's add a check in the setter:

class User { #name; set name(name) { if (name.length > 0) { this.#name = name; } else { throw new Error('name is incorrect'); } } get name() { return this.#name; } }

Let's create an object of the class:

let user = new User;

Let's write the data to our property:

user.name = 'john';

Let's try to write an incorrect line and get an error:

user.name = '';

Implement setters for the properties of class Employee.

Add checks to the setters of the Employee class accessors.

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