forked from tzeikob/javascript-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (35 loc) · 907 Bytes
/
index.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
// Use your own namespace to keep global scope clean
var myNS = myNS || Object.create(null);
myNS.Entity = class Entity {
constructor(x, y) {
// Here you can set state to the object
this.x = x;
this.y = y;
}
// Add member methods shared across objects
methodA() {
return this.x + this.y;
}
methodB() {
return Math.min(this.x, this.y);
}
methodC(z) {
return Math.max(this.x, this.y, z);
}
// Add static methods as well
static clone(other) {
return new Entity(other.x, other.y);
}
};
let e1 = new myNS.Entity(266, 4),
e2 = new myNS.Entity(3, 145);
let a1 = e1.methodA(),
b1 = e1.methodB(),
c1 = e1.methodC(1024);
let a2 = e2.methodA(),
b2 = e2.methodB(),
c2 = e2.methodC(-55);
console.log(a1, b1, c1); // 270 4 1024
console.log(a2, b2, c2); // 148 3 145
let clone = myNS.Entity.clone(e1);
console.log(clone); // Entity { x: 266, y: 4 }