Skip to content

Instantly share code, notes, and snippets.

@vstarck
Last active December 12, 2015 01:29
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 vstarck/4691776 to your computer and use it in GitHub Desktop.
Save vstarck/4691776 to your computer and use it in GitHub Desktop.
Emulating private properties using ES6 proxies
var myObject = {
__meta__: {
public: ['publicMethod']
},
privateMethod: function() { return 'private'},
publicMethod: function() {return 'public' }
};
var myProxyObject = new Proxy(myObject, {
get: function(target, name) {
if(target.__meta__.public.indexOf(name) === -1) throw new Error('THIS IS PRIVAAAAAAAATE');
return target[name];
}
});
myProxyObject.publicMethod(); // "public"
myProxyObject.privateMethod(); // Error: THIS IS PRIVAAAAAAAATE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment