⊗jsOpBsPS 17 of 60 menu

Setters in OOP in JavaScript

To write private properties, methods are also made, which are called setters. Their names should begin with the word set. Let's make property setters:

class User { #name; #surn; setName(name) { this.#name = name; } setSurn(surn) { this.#surn = surn; } getName() { return this.#name; } getSurn() { return this.#surn; } }

Let's check the work of getters and setters. Let's create an object of our class:

let user = new User;

Using setters, we set the values ​​of properties:

user.setName('john'); user.setSurn('smit');

Using getters, we will output the property values:

console.log(user.getName()); console.log(user.getSurn());

Add property setters to your 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