-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetNset.js
98 lines (82 loc) · 2.17 KB
/
getNset.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* get -> read a property
* set -> set a property
*
* helps with validation and validating when reading or writing a property
*/
// class Rectangle {
// constructor(width, height) {
// this.width = width;
// this.height = height;
// }
// set width(newWidth) {
// if (newWidth > 0) {
// this._width = newWidth; // _ as prefix means that is private property, dont touch it
// } else {
// console.error("Width must be a positive number");
// }
// }
// set height(newHeight) {
// if (newHeight > 0) {
// this._height = newHeight; // _ as prefix means that is private property, dont touch it
// } else {
// console.error("height must be a positive number");
// }
// }
// get width() {
// return this._width;
// }
// get height() {
// return this._height;
// }
// get area() {
// return this._height * this._width
// }
// }
// const rec = new Rectangle(2, 4)
// rec.width = 5
// console.log(rec.width)
// console.log(rec.height)
// console.log(rec.area)
class Person {
constructor(firstName, lastName, age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
set firstName(newFirstName) {
if (typeof newFirstName === "string" && newFirstName.length > 0) {
this._firstName = newFirstName;
} else {
console.error(
"first name must be a name and not null",
typeof firstName === "string"
);
}
}
set lastName(newLastName) {
if (typeof newLastName === "string" && newLastName.length > 0) {
this._lastName = newLastName;
} else {
console.error("last name must be a name and not null", typeof lastName);
}
}
set age(newAge) {
if (typeof newAge === "number" && newAge >= 0) {
this._age = newAge;
} else {
console.error("Age must be a number")
}
}
get firstName() {
return this._firstName
}
get lastName() {
return this._lastName
}
get fullName() {
return this._firstName + " " + this._lastName;
}
}
const person = new Person("John", "Square", 38)
console.log(person.fullName)