Created
August 7, 2020 11:02
-
-
Save minedun6/f19485267976ba644c38f9a068643f88 to your computer and use it in GitHub Desktop.
Seems like a cool trick that not too many people know about...Using PHP magic methods in JS, thanks to weird constructor semantics and Proxy. @assertchris
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
class Env { | |
constructor() { | |
this.secrets = {}; | |
return new Proxy(this, { | |
set: this.__set, | |
get: this.__get, | |
}); | |
} | |
__set(obj, name, value) { | |
return (obj.secrets[name] = value); | |
} | |
__get(obj, name) { | |
return obj.secrets[name]; | |
} | |
} | |
// example | |
var env = new Env(); | |
// can get + set secrets now | |
env.key = '5ec4e7s'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment