prv
Private fields in Javascript
This NPM module can be used to associate private data with objects. It stores an associated object with the private data in a WeakMap, keyed on the subject. If the subject is garbage collected, so is the private data.
This module works in Node JS (v4 and up) and all browsers that have support for WeakMap (all modern browsers).
Install
npm install --save prv
Include in your app
require
const createAccessor = const prv = // or, shorthandconst prv = // <-- note the double braces
import
const prv =
Copy-paste
Since this module is super-small, it might be beneficial for you to just copy-paste it into your codebase verbatim:
const createAccessor = { const p = return { if ! p p return p }}
Yup, that's all there is to it!
Usage
Create an accessor function and use it to associate private data with objects. Only code that has access to the accessor function can reach the private data.
const prv = { x = x y = y } { return x } { return y } const point = 00console // 0console // 0 // attempting to set x// ignored in 'sloppy' mode// throws TypeError in strict modepointx = 10
Issues
Add an issue in the issue tracker to let me know of any problems you find, or questions you may have.
Copyright
Copyright 2018 by Stijn de Witt. Some rights reserved.
License
Licensed under the Creative Commons Attribution 4.0 International (CC-BY-4.0) Open Source license.