-
Notifications
You must be signed in to change notification settings - Fork 5
/
constructor.js
97 lines (80 loc) · 2.41 KB
/
constructor.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
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
// Ex1: Creating a new class (declaration form)
// =======================================================
class Polygon {
constructor(height, width) {
this.name = 'Polygon';
this.height = height;
this.width = width;
}
sayName() {
console.log('Hi, I am a ', this.name + '.');
}
sayHistory() {
console.log('"Polygon" is derived from the Greek polus (many) ' + 'and gonia (angle).');
}
}
let polygon = new Polygon(300, 400);
console.log('I was created with a Class Declaration.');
polygon.sayName();
console.log('The width of this polygon is ' + polygon.width);
// Ex2: Creating a new class (expression-form)
// =======================================================
const MyPoly = class Poly {
getPolyName() {
console.log('Hi. I was created with a Class Express. My name is ' + Poly.name);
};
};
let inst = new MyPoly();
inst.getPolyName();
// Ex3: Extend an exsting class
// =======================================================
class Square extends Polygon {
constructor(length) {
super(length, length);
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
set area(value) {
this.area = value;
}
}
let square = new Square(5);
square.sayName();
console.log('The area of this square is ' + square.area);
// Example 4: Subclassing methods of a parent class
// ===============================================================
class Rectangle extends Polygon {
constructor(height, width) {
super(height, width);
this.name = 'Rectangle';
}
sayName() {
console.log('Sup! My name is ', this.name + '.');
super.sayHistory();
}
}
let rectangle = new Rectangle(50, 60);
rectangle.sayName();
// Example 5: Defining static methods
// ===============================================================
// Classes support static members which can be accessed without an
// instance being present.
class Triple {
static triple(n) {
n = n || 1;
return n * 3;
}
}
// super.prop in this example is used for accessing super-properties from
// a parent class. This works fine in static methods too:
class BiggerTriple extends Triple {
static triple(n) {
return super.triple(n) * super.triple(n);
}
}
console.log('Static method');
console.log(Triple.triple());
console.log(Triple.triple(6));
console.log(BiggerTriple.triple(3));