Skip to content

Instantly share code, notes, and snippets.

@aloerina01
Last active March 12, 2017 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloerina01/31a4ec7d91b94f5faccda0c4a9aa5f50 to your computer and use it in GitHub Desktop.
Save aloerina01/31a4ec7d91b94f5faccda0c4a9aa5f50 to your computer and use it in GitHub Desktop.
ES6 Proxy - ProxyModule
import SampleModule from './SampleModule';
let sample = new SampleModule();
console.log(sample.a); // public
console.log(sampple._a); // Uncaught TypeError: _a is private valiable
class SampleModule {
constructor() {
this._a = 'private';
this.a = 'public';
}
}
const handler = {
construct(target, args) {
return new Proxy(Reflect.construct(target, args), handler);
},
get(obj, prop) {
if (prop.indexOf('_') === 0) {
throw TypeError(`${prop} is private valiable`);
} else {
Reflect.get(obj, prop);
}
}
}
export default new Proxy(Sample, handler);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment