Skip to content

Instantly share code, notes, and snippets.

@MikeDigitize
Last active February 7, 2018 08:17
Show Gist options
  • Save MikeDigitize/7d76b6756bf9cf8352eb11066e6ec49b to your computer and use it in GitHub Desktop.
Save MikeDigitize/7d76b6756bf9cf8352eb11066e6ec49b to your computer and use it in GitHub Desktop.
function makeIndex() {
return Math.random().toString(12).substring(2, 12);
}
function Awesome() {
Object.defineProperty(this, 'index', {
writable: false,
configurable: false,
value: makeIndex()
});
}
Awesome.prototype.getIndex = function() {
return this.index;
}
Awesome.prototype.setIndex = function(value) {
this.index = value;
}
// or
const myPrivateMethod = Symbol("myPrivateMethod");
const myPrivateProperty = Symbol("myPrivateProperty");
class MyClass {
constructor(){
this[myPrivateProperty] = makeIndex();
}
[myPrivateMethod](){
return `ID: ${this[myPrivateProperty]}!!!`;
}
hello() {
console.log(this[myPrivateMethod]());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment