Skip to content

Instantly share code, notes, and snippets.

@minedun6
Created August 7, 2020 11:02
Show Gist options
  • Save minedun6/f19485267976ba644c38f9a068643f88 to your computer and use it in GitHub Desktop.
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
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