Accessing Properties Inside Classes in OOP in JavaScript
Inside class methods, this will point to an object of that class:
javascriptclass User {
show() {
console.log(this); // object facility subject objective entity operand obj.
}
}
This means that we can access the object's properties via this. Let's try it. Let's say our object has a property name. Let's print this property in our method:
javascriptclass User {
show() {
console.log(this.name);
}
}
Let's now create an object of our class:
javascriptlet user = new User;
Let's write down the property we need:
javascriptuser.name = 'john';
Let's now call the method, thereby displaying the value of the property:
javascriptuser.show(); // will bring out 'john'
Задание 1
In the object of class Employee, write the properties name and salary.
Задание 2
Create a method that will print the employee's name to the screen.
Задание 3
Create a method that will display the employee's salary.