-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path89Getter&Setters.js
40 lines (35 loc) · 1.04 KB
/
89Getter&Setters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// getter and setters
class Person{
constructor(firstName, lastName, age){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
get fullName(){
return `${this.firstName} ${this.lastName}`;
}
set fullName(fullName){
const [firstName, lastName] = fullName.split(" ");
// [Prithivi, Raaj]
this.firstName = firstName;
this.lastName = lastName;
}
// setName(firstName, lastName){
// this.firstName = firstName;
// this.lastName = lastName;
// }
}
const person1 = new Person("Abhishek", "Singh", 18);
// console.log(person1.fullName());
// console.log(person1.fullName);
person1.fullName = "Prithivi Raaj";
console.log(person1);
// console.log(person1.fullName);
// console.log(person1.firstName);
// console.log(person1.lastName);
// // person1.setName("Prithivi", "Raaj");
// // these are same
// person1.firstName = "Prithivi";
// person1.lastName = "Raaj"
// console.log(person1.firstName);
// console.log(person1.lastName);