-
Notifications
You must be signed in to change notification settings - Fork 1
/
classes.js
109 lines (90 loc) · 1.88 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
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
/*
* Basics.
*
* Use of semicolon ";" in end of lines is optional.
*
* Underscore.js used to provide easy to use tools.
*/
// define class with parameters
class BaseClass {
a = null
b = 10
var x = "hidden property of BaseClass"
}
obj = BaseClass()
// -->
function BaseClass() {
var self = this;
// properties
self.a = null;
self.b = 10;
var x = "hidden property of BaseClass";
}
obj = new BaseClass();
// define class with parameters and constructor
class Base2Class {
a = null
b = 10
var x = "hidden property of Base2Class"
function construct(a, b) {
self.a = a
self.b = b
}
function add() {
return self.a + self.b
}
function get_x() {
return x
}
}
obj = Base2Class(10, 20)
// -->
function Base2Class(a, b) {
var self = this;
// properties
self.a = null;
self.b = 10;
var x = "hidden property of Base2Class";
// constructor
self.a = a;
self.b = b;
// method add()
self.add = function add() {
return self.a + self.b;
}
// method get_x()
self.get_x = function get_x() {
return _x;
}
}
obj = new Base2Class(10, 20);
// class inheritance
class MyClass1 extends Base2Class {
c = "ccc"
var y = "hidden property of MyClass1"
// redefine method of parent's class
function get_x() {
return "Redefined method get_x() of parent's class"
}
function get_y() {
return y
}
}
obj = MyClass1(10, 20)
// -->
function MyClass1() {
var self = this;
// properties
self.c = "ccc";
var y = "hidden property of MyClass1";
// method get_x()
self.get_x = function get_x() {
return "Redefined method get_x() of parent's class";
}
// method get_y()
self.get_y = function get_y() {
return y;
}
}
_.extend(MyClass1, Base2Class);
obj = new MyClass1(10, 20);