# singleton.js

> JavaScript Singletons made easy.

Latest version **0.3.1** (published 2014-04-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install singleton.js
pnpm add singleton.js
yarn add singleton.js
bun add singleton.js
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2014-04-25 |
| First published | 2013-08-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.10.x |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Cory Martin |
| Maintainers | corymartin |
| Keywords | singleton, design patterns |

## Links

- npm: https://www.npmjs.com/package/singleton.js
- Repository: git@github.com:corymartin/singleton.js
- Homepage: https://github.com/corymartin/singleton.js
- Issues: https://github.com/corymartin/singleton.js/issues
- npm.io page: https://npm.io/package/singleton.js

## Recent versions

- 0.3.1 (latest) — 2014-04-25
- 0.3.0 — 2014-04-25
- 0.2.0 — 2013-08-16
- 0.1.0 — 2013-08-05

## README

Singleton.js
============

JavaScript Singletons made easy.


Installation
------------

```bash
$ npm install singleton.js
```

```js
var singleton = require('singleton.js');
```

Browser
-------

ES5 browser (>= IE9) required.

[singleton.js](https://rawgithub.com/corymartin/singleton.js/master/dist/singleton.js)

[singleton.min.js](https://rawgithub.com/corymartin/singleton.js/master/dist/singleton.min.js)

API
---

### singleton(Ctor)

__arguments__

- `Ctor` Optional. Function constructor.

__returns__

- Singleton function constructor.
    - Returns initial instance for all subsequent instantiations.
    - `new` is optional.
    - Inherits from passed function constructor.
    - Will have own properties of passed function constructor.

```js
var Modal = singleton();
Modal.prototype = {
  open:  function() { /****/ },
  close: function() { /****/ }
};
new Modal() === new Modal()  // => true
new Modal() instanceof Modal // => true
```

```js
function Modal(priority){
  this.priority = priority;
}
Modal.prototype.open  = function() { /****/ };
Modal.prototype.close = function() { /****/ };

// Create a singleton from an existing function constructor
var Alert = singleton(Modal);
new Alert(10);
new Alert() === new Alert()  // => true
// `new` is optional
Alert() === new Alert()      // => true
new Alert() instanceof Alert // => true
new Alert() instanceof Modal // => true
new Alert().priority         // => 10
new Alert(3).priority        // => 10
```

```js
// Define singleton's constructor
var Printer = singleton(function(name) {
  this.name  = name;
  this.queue = [];
});
Printer.prototype.print = function() { /****/ };

new Printer('Office Printer');
new Printer().name        // => 'Office Printer'
new Printer('Chuck').name // => 'Office Printer'
```

Example with Backbone.js Collections

```js
var Movie = Backbone.Model.extend();

var Movies = singleton(Backbone.Collection.extend({
  model: Movie
}));

// Alternatively
// var Movies = singleton(Backbone.Collection);
// Movies.prototype.model = Movie;

new Movies() === new Movies() // => true

new Movies().add([
  {id: 100, title: 'Gattica'},
  {id: 101, title: 'Batman Begins'}
]);

new Movies().length // => 2

// Static/Own properties are maintained
var BMovies = Movies.extend();
new BMovies() instanceof Movies              // => true
new BMovies() instanceof Backbone.Collection // => true
```

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