Last active
December 12, 2015 01:29
-
-
Save vstarck/4691776 to your computer and use it in GitHub Desktop.
Emulating private properties using ES6 proxies
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
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