DEV Community

Discussion on: Show me your best Open Source project

Collapse
 
jankapunkt profile image
Jan Küster • Edited

GitHub logo jankapunkt / js-class-privacy

Create a given ES6 class with private members using Proxy and closures. Keeps class code clean, encourages SRP and DRY.

🔒 Javascript Class-Privacy

Build Status JavaScript Style Guide Project Status: Active – The project has reached a stable, usable state and is being actively developed. npm bundle size

Lean dry no-dep srp :cup: package to create instances from classes with defined private members Keep your classes clean und use this instead to define private properties Uses proxies to hide information.

Installation and basic usage

Install this package via NPM like

$ npm install class-privacy
Enter fullscreen mode Exit fullscreen mode

The packages exports only one function, that acts similar to an abstract factory You can pass in a decide function to define rules (e.g. whitelist) for members. The created factory can be used to create (proxies to) instances that contain only the public members.

import createFactory from 'class-privacy'
export class Person {
  constructor ({ name, age }) {
    this.name = name
    this.age = age
  }

  greet () {
    return `Hello, my name is "${this.name}". I am ${this.age} years old.`
  }
}

// make
Enter fullscreen mode Exit fullscreen mode

Uses Proxy to control private/public fields or methods of any JavaScript class. Real private and 100% not accessible. Works with ES6 classes as well as "Classic" classes.