Singletons in ES6 – The Good, The Bad, The Ugly
Tags: javascript, programming, tutorial, uncategorized
Published: 2019-11-04 16:56:44

---
title: Singletons in ES6 –  The Good, The Bad, The Ugly
published: true
date: 2019-11-04 16:54:17 UTC
tags: Javascript,Programming,Tutorial,Uncategorized
canonical_url: https://qvault.io/2019/11/04/singletons-in-es6-the-good-the-bad-the-ugly/
---

![](https://qvault.io/wp-content/uploads/2019/11/photo-1473655443462-4c2bdebc3e1c-1024x681.jpeg)

Singletons are fairly controversial as far as I can tell, especially in Javascript programming. Let’s take a look at what they are, when to (maybe) use them, and when not to.

## What is a Singleton?

> A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance. It contains static variables that can accommodate unique and private instances of itself. It is used in scenarios when a user wants to restrict instantiation of a class to only one object. This is helpful usually when a single object is required to coordinate actions across a system.
> 
> <cite><a href="https://www.techopedia.com/definition/15830/singleton">techopedia</a></cite>

Usually in object-oriented programming the idea is to define classes and create multiple instances of that class, each with their own state. This keeps code [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) and easy to maintain.

By contrast, a singleton can only be instantiated once, and therefore any code that accesses the singleton will either:

- Create a new instance
- Read, update, or modify that instance

Which means a singleton is _almost_ a global instance of a class. Gross.

## I Can’t Believe It’s Not Global!

![](https://qvault.io/wp-content/uploads/2019/11/large_22382829-7658-4a4b-8759-47b0276cc067.png)

For the purpose of this article, we will assume we are using ES6 modules in our front-end React or Vue project. An example of a singleton we might want could be:

```
// Define our state and initialize it
let darkMode = false;

// Define the functions that will expose that state
const Settings = {
  setDarkMode: (newVal) => darkMode = newVal,
  getDarkMode: () => darkMode,
}

// Disallow new properties on our object
Object.freeze(Settings);

export default Settings;
```

As stated earlier, a singleton is dangerously close to being a global variable, and we don’t like those. There is one important difference:

The singleton instance **isn’t actually globally scoped** : in order to modify state the caller must import the class and use getters/setters. This makes access more explicit and controlled. Not only are the ways in which the state can be modified explicitly defined, but files that use the state must ‘import’ it.

## But Muh Redux

Here’s my over-simplified opinion in the form of a flowchart:

![](https://qvault.io/wp-content/uploads/2019/11/Screen-Shot-2019-11-04-at-9.29.33-AM.png)

The idea is to use the simplest, most-controlled solution we reasonably can. In order of least evil –\> most evil:

- scoped constants
- scoped variables
- singletons
- redux/vuex
- global variables

## Singletons Suck

Redux, vuex, singletons and globals all suck. Try not to use them. I’m just trying to point out that they all suck to varying degrees. Good luck. Here are some additional resources on how much we don’t like singletons:

[https://blogs.msdn.microsoft.com/scottdensmore/2004/05/25/why-singletons-are-evil/](https://blogs.msdn.microsoft.com/scottdensmore/2004/05/25/why-singletons-are-evil/)

> [JavaScript Design Patterns: The Singleton](https://www.sitepoint.com/javascript-design-patterns-singleton/)

<iframe title="“JavaScript Design Patterns: The Singleton” — SitePoint" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" src="https://www.sitepoint.com/javascript-design-patterns-singleton/embed/#?secret=m7zvyUyrZV" data-secret="m7zvyUyrZV" width="600" height="338" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>

[https://alligator.io/js/js-singletons/](https://alligator.io/js/js-singletons/)

By Lane Wagner [@wagslane](https://twitter.com/wagslane)

Download Qvault: [https://qvault.io](https://qvault.io/)

Star our Github: [https://github.com/q-vault/qvault](https://github.com/q-vault/qvault)

The post [Singletons in ES6 – The Good, The Bad, The Ugly](https://qvault.io/2019/11/04/singletons-in-es6-the-good-the-bad-the-ugly/) appeared first on [Qvault](https://qvault.io).