https://github.com/averagemarcus/privatise
Create private properties using the underscore naming convention
https://github.com/averagemarcus/privatise
hacktoberfest
Last synced: 9 months ago
JSON representation
Create private properties using the underscore naming convention
- Host: GitHub
- URL: https://github.com/averagemarcus/privatise
- Owner: AverageMarcus
- License: mit
- Created: 2018-02-04T11:41:52.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-13T06:26:03.000Z (over 8 years ago)
- Last Synced: 2025-08-22T10:07:28.708Z (11 months ago)
- Topics: hacktoberfest
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@averagemarcus/privatise
- Size: 113 KB
- Stars: 22
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Privatise [](https://travis-ci.org/AverageMarcus/privatise)
> 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
```sh
$ npm install --save @averagemarcus/privatise
```
> Note: Requires Node 6+.
## Examples
Privatise an instance:
```js
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:
```js
// 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
* [Marcus Noble](http://github.com/averageMarcus/)
## License
MIT