Skip to content

lancedikson/singletone

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Singletone

Powerful and easy to use JavaScript implementation of the Singleton design pattern. It creates singleton instance which nobody can to change or replace. Now it supports Node.js (CommonJS modules) only, but frontend support needed (AMD, etc.). Help wanted.

Install

npm i --save singletone

Examples

var singletone = require('singletone');

function Player() {
  // save the instance local variable for the moment
  var singletonInstance = singletone(this);

  // rewrite our singleton constructor
  Player = singletonInstance.constructor;

  // add some instance properties
  this.status = "stopped";

  // return singleton instance
  return singletonInstance;
}

Player.prototype.play = function () {
  this.status = "playing";
};

Player.prototype.stop = function () {
  this.status = "stopped";
};

var player = new Player();

player.play();

console.log(player.status);
// "playing"

var player2 = Player();

console.log(player2.status);
// "playing"

For more information and examples look at the tests.

Contribution

If you want to send some PR, checkout develop branch please. It contains the latest commits.

About

Powerful and easy to use JavaScript implementation of the Singleton design pattern

Resources

Stars

Watchers

Forks

Packages

No packages published