Skip to content

Instantly share code, notes, and snippets.

@Sheraff
Last active January 6, 2020 19:25
Show Gist options
  • Save Sheraff/ee36ddc1dc44a506b7d2ca956dabf7c6 to your computer and use it in GitHub Desktop.
Save Sheraff/ee36ddc1dc44a506b7d2ca956dabf7c6 to your computer and use it in GitHub Desktop.
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