private-class
Private fields in js classes
This module allow to protected private properties of classes.
It work by creating a new prototype that wrap each methods of the original one.
On creation of a new instance of the wrapped prototype, a new instance of the original class is created and saved in a WeakMap. Wrapped methods retrieve the original class instance from there and forward the class to its methods.
Usage
const priv = ; const Person = ; const p = 'Andrea' 40;// public function are visibleconsole;// <-- function // public getter & setter are visiblepage = 41;console;// <-- 41 // public function can read private fieldsconsole;// <-- Andrea // extern code can not read private fieldsconsole;// <-- undefinedconsole;// <-- undefined
API
const privateClass = : function
Given a class, return a new class that doesn't allow to access private properties.
Install
With npm installed, run
$ npm install private-class
See Also
License
MIT