1.1.1 • Published 5 years ago

@coryjamescrook/singleton v1.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

singleton

A simple design pattern for a singleton class in javascript.

To use

Simply install:

npm install @coryjamescrook/singleton

Import into your project:

import Singleton from '@coryjamescrook/singleton'

or

const Singleton = require('@coryjamescrook/singleton')

Extend your singleton class from it:

class Test extends Singleton {
  constructor() {
    super()
    this.name = "TEST"
  }

  sayMyName() {
    console.log(`Hi! My name is ${this.name}`)
  }
}

Get the singleton instance of your class:

const t = new Test
// only one instance of the object will ever be created

Try it out:

console.log(t.sayMyName())

Voila!