forked from mohsinkd786/js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.js
69 lines (64 loc) · 1.72 KB
/
User.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
class User{
// no argument constructor
/*constructor(){
console.log('Param less constructor');
}*/
// paramerized constructor
constructor(title,body){
this.title = title;
this.body = body;
}
// get the generated message
getMessage(){
return {
title : this.title,
body: this.body
}
}
getUser(){
console.log('getUser() : User class');
}
static getTitleAndMessage(){
return this.title + " "+ this.body;
}
}
// inheritance
class Employee extends User {
// private members
//#userID;
constructor(title,body){
// call parent class constructor i.e. User class constructor
super(title,body);
}
getEmployee(){
//this.getUser();
console.log('Employee');
}
getUser(){
console.log('getUser() : Employee class');
// refer to parent instance / class
super.getUser();
}
}
const inherit = ()=>{
const emp = new Employee('Employee','Friday is a tough day');
// parent class member access via child i.e Employee extend User
console.log(emp.getMessage());
// child class : i.e. Employee
emp.getEmployee();
emp.getUser();
}
//
const verify = ()=>{
//const userObj = new User();
const userObj1 = new User('User','User seems to be confused');
console.log(JSON.stringify(userObj1.getMessage()));
// static method call
console.log(User.getTitleAndMessage());
// JSON Conversions
const customMessage = `{"title":"Custom","body":"This is a custom built JSON"}`;
console.log(customMessage);
// convert into JSON
const customMessageObj = JSON.parse(customMessage);
console.log(customMessageObj);
}