-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-14_CLASSES.js
More file actions
146 lines (89 loc) · 2.48 KB
/
Day-14_CLASSES.js
File metadata and controls
146 lines (89 loc) · 2.48 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Activity 1: Class Definition
// Task 1:
class Person {
constructor(fname, lname, age) {
this.fname = fname;
this.lname = lname;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.fname}!`);
}
updateAge (newAge) {
this.age = newAge;
console.log(`I have updated my age to ${this.age}!`);
}
static staticGreet () {
console.log("Hello, my name is a static function!");
}
get fullName() {
return `The Full Name is ${this.fname} ${this.lname}...`
}
set updateName (name) {
this.fname = name.fname;
this.lname = name.lname;
console.log(`I have updated my name to ${this.fname} ${this.lname}!`);
}
};
const sahil = new Person("Sahil", 22);
sahil.greet();
// Task 2:
sahil.updateAge(23);
sahil.newfunc = function () {
console.log("This is a new function in the Person class");
}
sahil.newfunc(); // This will log "This is a new function in the Person class" to the console.
// Activity 2: Inheritance
// Task 3:
class Student extends Person {
constructor(fname, age, id) {
super(fname, age);
this.id = id;
Student.numberOfStudent++;
}
getId() { return this.id; }
greet() {
console.log(`Hello, my name is ${this.name}! I am a student with ID ${this.getId()}`);
}
static numberOfStudent = 0;
}
const student = new Student("Sahil", 20, 123456);
console.log(student.getId());
// Task 4:
student.greet();
// Activity 3: Static Methods and Properties
// Task 5:
Person.staticGreet();
// Task 6:
console.log(`Number of Students: ${Student.numberOfStudent}`)
const newStudent = new Student("Coffee", 10, 52146);
console.log(`Number of Students: ${Student.numberOfStudent}`)
// Activity 4: Getters and Setters
// Task 7:
const newPerson = new Person("Chai,", "Code", 4);
console.log(newPerson.fullName);
// Task 8
newPerson.updateName = { fname: "Coffee", lname: "Code" };
console.log(newPerson.fullName);
// Activity 5: Private Fields (Optional)
// Task 9:
class Account {
#Balance = 0;
deposite (amount) {
if (amount > 0){
this.#Balance += amount;
}
}
withdraw (amount) {
if ( amount < this.#Balance && amount > 0){
this.#Balance -= amount
}
}
getBalance () {
return this.#Balance;
}
}
// Task 10:
const account = new Account();
account.deposite(1000);
console.log(account.getBalance());