-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathclass.js
74 lines (62 loc) · 2.65 KB
/
class.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
// static methods and properties of classes will be hidden under Symbol('staticNames')
const staticNamesProperty = typeof Symbol === 'function' ? Symbol('staticNames') : '__staticNames';
const { getOwnPropertySymbols } = Object;
const { hasOwnProperty } = Object.prototype;
export default function Class(prototype, staticProps) {
const Constructor = hasOwnProperty.call(prototype, 'constructor')
? prototype.constructor
: function EmptyConstructor() {};
// extends is kept for backward compatibility
const Parent = prototype.extends;
// inherit proto from class parent or empty object
const proto = Object.create(Parent ? Parent.prototype : {});
const parentStaticNames = Parent ? Parent[staticNamesProperty] : undefined;
nofn.assign(proto, prototype);
// allow to pass symbols as prototype properties
if (getOwnPropertySymbols) {
const symbols = getOwnPropertySymbols(prototype);
nofn.forEach(symbols, (symbol) => {
proto[symbol] = prototype[symbol];
});
}
// inherit staric properties of a parent
if (typeof parentStaticNames === 'object') {
const staticNames = Constructor[staticNamesProperty] || {};
Constructor[staticNamesProperty] = staticNames;
nofn.forOwn(parentStaticNames, (_, name) => {
Constructor[name] = Parent[name];
staticNames[name] = true;
});
// inherit static properties of a parent when their keys are symbols
if (getOwnPropertySymbols) {
const symbols = getOwnPropertySymbols(parentStaticNames);
nofn.forEach(symbols, (symbol) => {
Constructor[symbol] = Parent[symbol];
staticNames[symbol] = true;
});
}
}
// extend Constructor with passed static properties
if (typeof staticProps === 'object') {
const staticNames = Constructor[staticNamesProperty] || {};
Constructor[staticNamesProperty] = staticNames;
nofn.forOwn(staticProps, (value, key) => {
Constructor[key] = value;
staticNames[key] = true;
});
// extend Constructor with passed static properties if their keys are symbols
if (getOwnPropertySymbols) {
const symbols = getOwnPropertySymbols(staticProps);
nofn.forEach(symbols, (symbol) => {
Constructor[symbol] = staticProps[symbol];
staticNames[symbol] = true;
});
}
}
Constructor.prototype = proto;
// if new Class({}) is called return its instance
if (this instanceof Class) {
return new Constructor();
}
return Constructor;
}