⊗tsSpOpAMPb 9 of 37 menu

Public modifier in TypeScript

Properties and methods declared via the public modifier are available for reading and writing outside the class. Let's see in practice. Let's make our class have a public property name:

class User { public name: string = ''; }

Let's create an object of the class:

let user = new User();

Let's change the value of the property:

user.name = 'john';

Let's read the property:

console.log(user.name);

Create a class User that will contain public properties with the user's name and year of birth. Create a public method getAge that will get the user's age based on their year of birth.

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