-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClasses.js
More file actions
202 lines (182 loc) · 5.72 KB
/
Copy pathClasses.js
File metadata and controls
202 lines (182 loc) · 5.72 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"use strict";
//------------------------------------------------------------------------------
// Class Definition
// More intuitive, OOP-style and boilerplate-free classes.
// http://es6-features.org/#ClassDefinition
//------------------------------------------------------------------------------
class Shape {
constructor (id, x, y) {
this.id = id
this.move(x, y)
}
move (x, y) {
this.x = x
this.y = y
}
}
//------------------------------------------------------------------------------
// Class Inheritance
// More intuitive, OOP-style and boilerplate-free inheritance.
// http://es6-features.org/#ClassInheritance
//------------------------------------------------------------------------------
class Rectangle extends Shape {
constructor (id, x, y, width, height) {
super(id, x, y)
this.width = width
this.height = height
}
}
class Circle extends Shape {
constructor (id, x, y, radius) {
super(id, x, y)
this.radius = radius
}
}
//------------------------------------------------------------------------------
// Class Inheritance, From Expressions
// Support for mixin-style inheritance by extending from expressions yielding
// function objects. [Notice: the generic aggregation function is usually
// provided by a library like this one, of course]
// http://es6-features.org/#ClassInheritanceFromExpressions
//------------------------------------------------------------------------------
var aggregation = (baseClass, ...mixins) => {
let base = class _Combined extends baseClass {
constructor (...args) {
super(...args)
mixins.forEach((mixin) => {
mixin.prototype.initializer.call(this)
})
}
}
let copyProps = (target, source) => {
Object.getOwnPropertyNames(source)
.concat(Object.getOwnPropertySymbols(source))
.forEach((prop) => {
if (prop.match(/^(?:constructor|prototype|arguments|caller|name|bind|call|apply|toString|length)$/))
return
Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop))
})
}
mixins.forEach((mixin) => {
copyProps(base.prototype, mixin.prototype)
copyProps(base, mixin)
})
return base
}
class Colored {
initializer () { this._color = "white" }
get color () { return this._color }
set color (v) { this._color = v }
}
class ZCoord {
initializer () { this._z = 0 }
get z () { return this._z }
set z (v) { this._z = v }
}
class Shape {
constructor (x, y) { this._x = x; this._y = y }
get x () { return this._x }
set x (v) { this._x = v }
get y () { return this._y }
set y (v) { this._y = v }
}
class Rectangle extends aggregation(Shape, Colored, ZCoord) {}
var rect = new Rectangle(7, 42)
rect.z = 1000
rect.color = "red"
console.log(rect.x, rect.y, rect.z, rect.color)
//------------------------------------------------------------------------------
// Base Class Access
// Intuitive access to base class constructor and methods.
// http://es6-features.org/#BaseClassAccess
//------------------------------------------------------------------------------
class Shape {
// …
toString () {
return `Shape(${this.id})`
}
}
class Rectangle extends Shape {
constructor (id, x, y, width, height) {
super(id, x, y)
// …
}
toString () {
return "Rectangle > " + super.toString()
}
}
class Circle extends Shape {
constructor (id, x, y, radius) {
super(id, x, y)
// …
}
toString () {
return "Circle > " + super.toString()
}
}
//------------------------------------------------------------------------------
// Static Members
// Simple support for static class members.
// http://es6-features.org/#StaticMembers
//------------------------------------------------------------------------------
class Rectangle extends Shape {
// …
static contextTypes = {
router: PropTypes.object,
};
static defaultRectangle () {
return new Rectangle("default", 0, 0, 100, 100)
}
}
class Circle extends Shape {
// …
static defaultCircle () {
return new Circle("default", 0, 0, 100)
}
}
var defRectangle = Rectangle.defaultRectangle()
var defCircle = Circle.defaultCircle()
//------------------------------------------------------------------------------
// Getter/Setter
// Getter/Setter also directly within classes (and not just within object
// initializers, as it is possible since ECMAScript 5.1).
// http://es6-features.org/#GetterSetter
//------------------------------------------------------------------------------
class Rectangle {
constructor (width, height) {
this._width = width
this._height = height
}
set width (width) { this._width = width }
get width () { return this._width }
set height (height) { this._height = height }
get height () { return this._height }
get area () { return this._width * this._height }
}
var r = new Rectangle(50, 20)
r.area === 1000
//------------------------------------------------------------------------------
// Class definition with empty statement
//------------------------------------------------------------------------------
class A {
;
}
////
class B {
get [runtimeCalc]() {return 1};
set [runtimeCalc](p) {};
get 'string as key'() {};
}
// extended object
let Obj = {
[asdfg](a){},
* foo () {},
f(){},
get a(){},
set a([aa]=123){},
...anotherObj,
...{
speradObjectLiteral
},
...functionResult()
}