Last active
January 6, 2020 19:25
-
-
Save Sheraff/ee36ddc1dc44a506b7d2ca956dabf7c6 to your computer and use it in GitHub Desktop.
This file contains 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
const privateKey = Symbol('secret') | |
class SymbolPrivate { | |
constructor() { | |
this[privateKey] = 'SECRET' | |
this.publicKey = 42 | |
} | |
get privateKey() { | |
return this[privateKey] | |
} | |
publicFunction() { | |
return this.publicKey | |
} | |
} | |
const instance = new SymbolPrivate() | |
const proxy = new Proxy(instance, { | |
get: function (target, key) { | |
Object.getOwnPropertySymbols(target).forEach(symbol => console.log(instance[symbol])) | |
return target[key] | |
} | |
}) | |
SymbolPrivate.prototype.publicFunction.call(proxy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment