-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.js
72 lines (58 loc) · 2.45 KB
/
classes.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
// TODO: Classes and module exports in JavaScript
class User{
constructor(name, email){
this.name = name;
this.email = email;
}
#courseList = []; //? with the # symbol to indicate private property
getInfo(){
return `Name: ${this.name}, Email: ${this.email}`;
}
enrollCourse(name) // Setter for courseList, here some parameter is usually required
{
this.#courseList.push(name);
}
getCourseList() // Getter for courseList, usually return something
{
return this.#courseList;
}
static login() //? this is a static method, it belongs to the class itself, not to an instance of the class, nor it is inherited by other classes
{
return "User logged in";
}
}
// let User1 = new User('John Wick', '[email protected]');
// console.log(User1);
//* Continuing with the above code...
// TODO: Classes and inheritance in JavaScript
class Admin extends User {
constructor(name, email) {
super(name, email);
}
getAdminInfo() {
return "I am an admin";
}
login() //? Overriding the login method from the parent class, and it will be called instead of the parent class's login method. when called when called from an instance of the child class, it will call the overridden method.
{
return "Admin logged in";
}
}
//* Exports User class
module.exports = User;
var User2 = new User('John Cena', '[email protected]');
// console.log(User2.getInfo());
User2.enrollCourse('C++');
User2.enrollCourse('C#');
User2.enrollCourse('Python');
// console.log(User2.getCourseList());
// console.log(User2.courseList); // Accessing private property directly is not allowed, if "courseList" was not a private property this will work fine. just like the above line.
const tom = new Admin("Tom Hanks", "[email protected]");
console.log(tom.getAdminInfo());
// console.log(tom.login()); //? static method can be accessed directly on the class itself, not on an instance of the class.
console.log(tom.getInfo());
console.log(User.login()); //? static method can only be called directly on the class itself, not on an instance of the class.
console.log(tom.login()); //? Overridden method will be called instead of the parent class's login method. when called from an instance of the child class, it will call the overridden method.
//!..............................................
// * TODO:
// ? FIXME:
// !