⊗jsOpBsCPP 12 of 60 menu

Properties via constructor parameters in OOP in JavaScript

Variables passed through constructor parameters can be written to the object's properties:

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

Thus, the passed values ​​will become available in all methods of the class. For example, let's use the passed values ​​in some method:

class User { constructor(name, surn) { this.name = name; this.surn = surn; } show() { return this.name + ' ' + this.surn; } }

Let's check how it works. Let's create a new object, passing the user's first and last name as a parameter:

let user = new User('john', 'smit');

Let us now turn to our method:

console.log(user.show());

Pass the employee's name and salary to the Employee class constructor and write them to the corresponding properties.

Create a method that will output the employee's name.

Create a method that will output the employee's salary.

Create a method that will increase the employee's salary by 10%.

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