# create-internal

> simple storage and access of private values, powered by WeakMaps

Latest version **1.1.0** (published 2017-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install create-internal
pnpm add create-internal
yarn add create-internal
bun add create-internal
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2017-09-12 |
| First published | 2016-12-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=4 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ron Waldon |
| Maintainers | jokeyrhyme |
| Keywords | class, internal, namespace, private, weakmap |

## Links

- npm: https://www.npmjs.com/package/create-internal
- Repository: https://github.com/jokeyrhyme/create-internal.js
- Homepage: https://github.com/jokeyrhyme/create-internal.js#readme
- Issues: https://github.com/jokeyrhyme/create-internal.js/issues
- npm.io page: https://npm.io/package/create-internal

## Recent versions

- 1.1.0 (latest) — 2017-09-12
- 1.0.1 — 2016-12-22
- 1.0.0 — 2016-12-21

## README

# create-internal.js [![npm](https://img.shields.io/npm/v/create-internal.svg?maxAge=2592000)](https://www.npmjs.com/package/create-internal) [![Travis CI Status](https://travis-ci.org/jokeyrhyme/create-internal.js.svg?branch=master)](https://travis-ci.org/jokeyrhyme/create-internal.js)

simple storage and access of private values, powered by WeakMaps


## Example

```js
const createInternal = require('create-internal');

// never export `internal`,
// as any code with access to it can access the "private" data within
const internal = createInternal();

class MyClass {
  constructor () {
    Object.assign(internal(this), {
      // accessible to code with this exact `internal` in scope
      privateProperty: 'value'
    });
    Object.assign(this, {
      // accessible to any code that can access the constructed object
      publicProperty: 'value'
    });
  }

  method () {
    const { privateProperty } = internal(this);
    // TODO: do something with privateProperty
  }
}
```


## Usage


We use [`WeakMap`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap) as the underlying store, which means that any value that is not a [primitive type](https://developer.mozilla.org/en-US/docs/Glossary/Primitive) can be a key, e.g. `Object`, `Array`, `Map`, `Set`, `Function`, etc


### `createInternal()`

returns a function that takes an object and returns a private object associated with it

```js
const createInternal = require('create-internal');
const internal = createInternal(); // never export this

const publicObject = {};
const privateObject = internal(publicObject);
privateObject.privateProperty = 'privateValue';
```


### `createMapInternal()`

returns a function that takes an object and returns a private `Map` associated with it

```js
const { createMapInternal } = require('create-internal');
const internal = createMapInternal(); // never export this

const publicObject = {};
const privateMap = internal(publicObject);
privateMap.set('privateProperty', 'privateValue');
```


## See Also

-   [Mozilla contributor's guide for private properties](https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Contributor_s_Guide/Private_Properties)

-   [namespace](https://www.npmjs.com/package/namespace)

---
_Source: https://npm.io/package/create-internal · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
