Create private properties using the underscore naming convention
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
index.js
package-lock.json
package.json
test.js

README.md

Privatise Build Status

Create private properties using the underscore naming convention

Privatise allows you to create private properties using the well established underscore prefix naming convention. Read and writes to any properties marked as private will result in an error being thrown.

Features

  • Prevent read and write access to properties prefixed with _
  • Prevent private properties from being enumerable
  • Filter out private variables when calling JSON.stringify
  • Ability to privatise clases or instances

Install

$ npm install --save @averagemarcus/privatise

Note: Requires Node 6+.

Examples

Privatise an instance:

import privatise from '@averagemarcus/privatise';

class TestClass {
  constructor() {
    this.publicInt = 1;
    this._privateInt = 2;
  }
}

const instance = privatise(new TestClass());
console.log(instance.publicInt); // 1
console.log(instance._privateInt); // Error: Attempt to access private property

Privatise a class:

// TestClass.js
import privatise from '@averagemarcus/privatise';

class TestClass {
  constructor() {
    this.publicInt = 1;
    this._privateInt = 2;
  }
}

module.exports = privatise(TestClass);

// index.js
import TestClass from './TestClass'
const instance = new TestClass();
console.log(instance.publicInt); // 1
console.log(instance._privateInt); // Error: Attempt to access private property

Created by

License

MIT