3.9K Followers
·
Follow

How to implement them and whether they’re actually useful

Image for post
Image for post
Photo by Scott Webb on Unsplash

A singleton is a function or class which can have only one instance. It’s a design pattern popularised by the “Gang of Four” in their influential Design Patterns. However, the singleton is one of several patterns in the book which have been criticised. Some are regarded as anti-patterns, while others are only useful in particular languages. So, what about singletons? And do they make sense JavaScript?

In this article, we’ll see how to implement singletons in JavaScript, before asking whether or not they’re useful in the language.

Class Singleton

Since Design Patterns focused on C++, we’re used to seeing singletons as classes. Since ES6, we’ve had a class syntax in JavaScript, so a basic implementation of a singleton could look something like…


How to combine two of the most popular tools in web development

Image for post
Image for post
Photo by Ben Kolde on Unsplash

Right now, Typescript and React and two of the most popular technologies in web development. In this article, we’ll look into using the two technologies in tandem, covering basic type checking in functional and class components. The article’s intended for anyone who’s bringing TypeScript and React together for the first time, but it should also be useful for those who need a quick reminder of the syntax.

What is React?

React (maintained by Facebook) is a declarative, component-based library. It makes it easy to build interactive, reusable components which — thanks to its virtual DOM — update in a highly efficient way.

What is Typescript?

Typescript (built by Microsoft) is a superset of JavaScript: it contains all the features of JavaScript, plus a variety of type-checking features reminiscent of strongly-typed languages like Java and C#. It makes code easier to read, understand and debug. …


Tricks with the Spread Operator and Destructuring Assignment Syntax

Image for post
Image for post
Photo by Anete Lūsiņa on Unsplash

Among the many useful features introduced in ES6, few have the power to knock off as many lines of traditional JavaScript code as the spread operator and destructuring assignment syntax. In ES5, for example, you may have required a dedicated function to filter out unique values. Now, it’s possible in a single line of code.

In this article, we’ll look at several occasions when the spread operator and destructuring can make your code more concise. But first, let’s recap what these features do with some basic examples.

What is destructuring assignment syntax?

This is a way concise way to turn values from arrays or properties from objects into variables. We’ll start with a couple of examples of array destructuring. …

About

Bret Cameron

Writer and developer based in London. On Medium, I write about JavaScript and web development 💻

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store