-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreateClass.js
More file actions
31 lines (26 loc) · 773 Bytes
/
createClass.js
File metadata and controls
31 lines (26 loc) · 773 Bytes
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
"use strict";
var ImmutableObject = require("./ImmutableObject");
module.exports = function(members) {
members = members || {};
var ctor;
if (typeof members.init === "function") {
ctor = function() {
var instance = Object.freeze(
(this instanceof ctor) ? this : Object.create(ctor.prototype)
);
instance = members.init.apply(instance, arguments);
if (!instance || !instance.__isImmutableObject__) {
throw new Error("init method must return an immutable object.");
}
return instance;
};
} else {
ctor = function() {
return Object.freeze(
(this instanceof ctor) ? this : Object.create(ctor.prototype)
);
};
}
ctor.prototype = ImmutableObject(members);
return ctor;
};