Advertisement
elena1234

Class Person ( JavaScript )

Nov 15th, 2021
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Person {
  2.     constructor(first, last) {
  3.         this.first = first;
  4.         this.last = last;
  5.     }
  6.  
  7.     get firstName() {
  8.         return this.first;
  9.     }
  10.  
  11.     set firstName(value) {
  12.         return this.first = value;
  13.     }
  14.  
  15.     get lastName() {
  16.         return this.last;
  17.     }
  18.  
  19.     set lastName(value) {
  20.         return this.last = value;
  21.     }
  22.  
  23.     get fullName() {
  24.         return `${this.firstName} ${this.lastName}`;
  25.     }
  26.  
  27.     set fullName(value) {
  28.         let [first, last] = value.split(' ');
  29.         if (first !== undefined && last !== undefined) {
  30.             this.first = first;
  31.             this.last = last;
  32.             return `${this.firstName} ${this.lastName}`;
  33.         }  
  34.     }
  35. }
  36.  
  37. let person = new Person("Albert", "Simpson");
  38. console.log(person.fullName); //Albert Simpson
  39. person.firstName = "Simon";
  40. console.log(person.fullName); //Simon Simpson
  41. person.fullName = "Peter";
  42. console.log(person.firstName); // Simon
  43. console.log(person.lastName); // Simpson
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement