|
| 1 | + |
| 2 | +class Person { |
| 3 | + |
| 4 | + constructor(firstName, lastName) { |
| 5 | + this._firstName = firstName; |
| 6 | + this.lastName = lastName; |
| 7 | + } |
| 8 | + |
| 9 | + get firstName() { |
| 10 | + console.log('getting first name'); |
| 11 | + return this._firstName; |
| 12 | + } |
| 13 | + |
| 14 | + set firstName(value) { |
| 15 | + console.log('setting first name'); |
| 16 | + this._firstName = value; |
| 17 | + } |
| 18 | + |
| 19 | + getFullName() { |
| 20 | + return this.firstName + ' ' + this.lastName; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class Student extends Person { |
| 25 | + |
| 26 | + static create(studentId, name) { |
| 27 | + const nameParts = name.split(' '); |
| 28 | + return new Student(studentId, nameParts[0], nameParts[1]); |
| 29 | + } |
| 30 | + |
| 31 | + constructor(studentId, firstName, lastName) { |
| 32 | + super(firstName, lastName); |
| 33 | + this.studentId = studentId; |
| 34 | + } |
| 35 | + |
| 36 | + getRecordInfo() { |
| 37 | + return this.studentId + ' ' + this.lastName + ', ' + this.firstName; |
| 38 | + } |
| 39 | + |
| 40 | + getFullName() { |
| 41 | + return super.getFullName().toUpperCase(); |
| 42 | + } |
| 43 | + |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +// const student1 = new Student(1, 'Bob', 'Smith'); |
| 48 | +const student1 = Student.create(1, 'Bob Smith'); |
| 49 | +student1.firstName = 'Seema'; |
| 50 | +console.log(student1.getFullName()); |
| 51 | +console.log(student1.getRecordInfo()); |
| 52 | + |
| 53 | +console.dir(student1); |
| 54 | + |
0 commit comments