Skip to content

Instantly share code, notes, and snippets.

@raypulver
Created November 8, 2016 18:02
Show Gist options
  • Save raypulver/8306610fd8af542fc32e5a2941e2b410 to your computer and use it in GitHub Desktop.
Save raypulver/8306610fd8af542fc32e5a2941e2b410 to your computer and use it in GitHub Desktop.
var log = console.log;
var assign = Object.assign;
var MyClass = (function () {
var state = new Map();
function makeDefaultState() {
return { a: 0 };
}
function getState() {
return state.get(this);
}
function MyClass(init) {
if (!(this instanceof MyClass)) return new MyClass(init);
state.set(this, assign(makeDefaultState(), init || {}));
}
function privateFunc(a) {
return getState.call(this).a += a;
}
assign(MyClass.prototype, {
publicFunc(a) {
return privateFunc.call(this, a);
}
});
return MyClass;
})();
var instance = MyClass({ a: 5 });
log(instance.publicFunc(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment