-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.html
174 lines (132 loc) · 3.91 KB
/
class.html
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Class ES6</title>
</head>
<body>
<h1 style="color: green">
Class (Substitude of object constructor function)
</h1>
</body>
<script>
// class person {
// // 1. Constructor Function
// constructor(fname, id) {
// // console.log("Automatical call while instance of person is created... ");
// this.firstName = fname;
// this.personId = id;
// }
// // 2. Prototype Method (Directly add to person prototype)
// sayHello() {
// return `Name is: ${this.firstName}`;
// }
// // 3. Static Method (Only access through class only, not object (Instance))
// static sayHi() {
// console.log("Hi..");
// }
// // 4. Static property
// static sProperty = "static property...";
// }
// let person1 = new person("Nehal", 600);
// console.log(person1.sayHello());
// // console.log(person1.sayHi()); // Error: static method does not exist in prototype chain
// // console.log(person1.sProperty); // Undefined: Object does not have this property
// console.log(person.sayHi());
// ========================================
// class creature {
// constructor(ls) {
// console.log("hello from creature constructor...");
// this.lifeSpan = ls;
// }
// breath() {
// console.log("breathing...");
// }
// getLifeSpan() {
// console.log(`Life span is ${this.lifeSpan}`);
// }
// sayHello(x) {
// console.log(`Hello from ${x}`);
// }
// }
// class person extends creature {
// constructor(lspan, fname) {
// console.log("hello from person constructor..");
// super(lspan);
// this.firstName = fname;
// }
// sayHello(x) {
// console.log(`Hello from ${x}`);
// }
// getMessage() {
// super.sayHello("Super Class");
// this.sayHello("Sub Class");
// console.log(
// `Name is ${this.firstName} and will have ${this.lifeSpan} CAD...`
// );
// }
// }
// class third extends person {
// constructor(ls, fn, num) {
// console.log("hello from third constructor...");
// super(ls, fn);
// this.number = num;
// }
// }
// let third1 = new third(60, "Nehal", 303);
// console.log(third1);
// ========================================
// class person {
// #firstName;
// constructor(name) {
// console.log("constructor function...");
// this.#firstName = name;
// }
// #sayHello() {
// console.log("Hello");
// }
// getName() {
// console.log(`${this.#firstName}`);
// this.#sayHello();
// }
// }
// let person1 = new person("Nehal");
// person1.getName();
// ========================================
// let methodBox = {
// sayHello() {
// console.log("Hello..");
// },
// sayBye() {
// console.log("Bye..");
// },
// };
// class person {
// constructor(fName, id) {
// console.log("This is constructor function call..");
// }
// }
// Object.assign(person.prototype, methodBox);
// let person1 = new person("Nehal", 600);
// person1.sayHello();
// ========================================
class person {
constructor(name) {
this.fName = name;
}
get getName() {
return `${this.fName}`;
}
set setName(newName) {
this.fName = newName.toUpperCase();
}
}
let person1 = new person("Nehal");
// person1.setName("Pintu");
person1.setName = "Pintu";
let temp = person1.getName;
console.log(person1);
console.log(temp);
</script>
</html>