-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.js
66 lines (45 loc) · 1.25 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
//classes are templates for creating objects
/*class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
*/
/* An important difference between function declarations and class declarations is that
while functions can be called in code that appears before they are defined,
classes must be defined before they can be constructed.*/
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
// Getter
get area() {
return this.calcArea();
}
// Method
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.calcArea()); // 100
//private in classes
class Sample {
#property;
constructor(){
this.#property = "sample data";
}
//private method (cannot be accessed directly using object.property())
#privateMethod() {
return 'hello world';
}
getPrivateMessage() {
return this.#property;
}
}
const object = new Sample();
// console.log(object.property); //=> undefined
// console.log(object.privateMethod); // undefined
console.log(object.getPrivateMessage()); //test