Created
November 8, 2016 18:02
-
-
Save raypulver/8306610fd8af542fc32e5a2941e2b410 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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