Skip to content
master
Switch branches/tags
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

Create private properties using the underscore naming convention

Topics

Resources

License

Packages

No packages published