Last active
March 12, 2017 11:57
-
-
Save aloerina01/31a4ec7d91b94f5faccda0c4a9aa5f50 to your computer and use it in GitHub Desktop.
ES6 Proxy - ProxyModule
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
import SampleModule from './SampleModule'; | |
let sample = new SampleModule(); | |
console.log(sample.a); // public | |
console.log(sampple._a); // Uncaught TypeError: _a is private valiable |
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
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