# simple-js-namespace

> Simple way of adding global namespaces to browser/nodeJS

Latest version **2.1.5** (published 2016-10-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-js-namespace
pnpm add simple-js-namespace
yarn add simple-js-namespace
bun add simple-js-namespace
```

## 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 | 2.1.5 |
| Published | 2016-10-13 |
| First published | 2016-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | cabalbl4 |
| Keywords | namespace |

## Links

- npm: https://www.npmjs.com/package/simple-js-namespace
- Repository: https://github.com/Cabalbl4/js-namespace
- Homepage: https://github.com/Cabalbl4/js-namespace#readme
- Issues: https://github.com/Cabalbl4/js-namespace/issues
- npm.io page: https://npm.io/package/simple-js-namespace

## Recent versions

- 2.1.5 (latest) — 2016-10-13
- 2.1.4 — 2016-10-12
- 2.1.3 — 2016-10-11
- 2.1.2 — 2016-10-11
- 2.1.1 — 2016-10-11
- 2.1.0 — 2016-10-11

## README

# simple-js-namespace
Small function to define JavaScript namespaces

##Install
`npm install --save simple-js-namespace`

##Enable
```javascript
require("simple-js-namespace");
```

##Usage example
```javascript
require("simple-js-namespace");

_namespace = "SOME.NESTED.NAMESPACE";
console.log(SOME);
console.log(SOME.NESTED);
console.log(SOME.NESTED.NAMESPACE);

SOME.NESTED.NAMESPACE.Test = function() {
};

_namespace.Test1 = function() {
};

console.log(SOME.NESTED.NAMESPACE);

_namespace = ".NESTME";

SOME.NESTED.NAMESPACE.NESTME.Test = function() {}
_namespace.Test1 = function() {}
console.log(SOME.NESTED.NAMESPACE);
```

##Usage description

Define and use a namespace (namespaces are dot-delimited by default)

```javascript
_namespace = "SOME.NEW.NAMESPACE"

SOME.NEW.NAMESPACE.MyClass = function {
};
//ES 2015
SOME.NEW.NAMESPACE.MyClass2 = class {
};
//Shorthand
_namespace.MyClass3 = ... //function / class / anything

//Assign current namespace to global (reset)
_namespace = global;

//Assign current namespace to already existing one
_namespace = SOME.NEW.NAMESPACE;
```

Under the hood, namespace is a nested object, i.e. 

```javascript
_namespace = "SOME.NEW.NAMESPACE"

///equals to:
if(! global.SOME) global.SOME = {};
if(! global.SOME.NEW) global.SOME.NEW = {};
if(! global.SOME.NEW.NAMESPACE) global.SOME.NEW.NAMESPACE = {};
```

### Dot notation
There is another way to nest namespaces, which, hovever, must be used with caution, as javascript preserves namespace between files.

```javascript
_namespace = "SOME.NEW"

SOME.NEW.TestClass = function() {
 ...
};

_namespace = ".NAMESPACE" //Starts with dot - will add namespace to the last defined
SOME.NEW.NAMESPACE.TestClass = function() {

};
```


### \_namespace as shorthand
Property accessor \_namespace points to the last defined namespace. It can be used to shorten definitions.

```javascript
_namespace = "SOME.NEW"

_namespace.TestClass = function() {
 ...
}; // same as SOME.NEW.TestClass = function ...



_namespace = ".NAMESPACE" //Starts with dot - will add namespace to the last defined
_namespace.TestClass = function() {
...
}; // same as SOME.NEW.NAMESPACE.TestClass = function
```
 
### \_namespace_root_context variable
A global property to hold default context to create namespaces in. Use with caution.

```javascript
let nsContainer = {};
_namespace_root_context = nsContainer;

_namespace = "SOME.NEW.NAMESPACE"

///now equals to:
if(! nsContainer.SOME) nsContainer.SOME = {};
if(! nsContainer.SOME.NEW) nsContainer.SOME.NEW = {};
if(! nsContainer.SOME.NEW.NAMESPACE) nsContainer.SOME.NEW.NAMESPACE = {};
//ETC

//Export it in NodeJS for example
module.exports = nsContainer;

///Do not forget to reset namespace root at the end of module or js file if you changed it, as it is a global property and its unexpected change may break other modules!
//For browser
_namespace_root_context = window;
//OR for NodeJS
_namespace_root_context = global;


```

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