Last active
February 7, 2018 08:17
-
-
Save MikeDigitize/7d76b6756bf9cf8352eb11066e6ec49b 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
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