How to Create Singleton Classes in JavaScript - Design Patterns Tutorial

preview_player
Показать описание
In this video tutorial I'll be showing you a technique to create a Singleton Class in JavaScript with ES6 classes.

Singleton classes are used when you only want a single instance of a class to be created - this is useful for things like "settings" or "environmental variable" accessors.

Support me on Patreon:

Follow me on Twitter @dcodeyt!

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

#dcode #javascript #webdev
Рекомендации по теме
Комментарии
Автор

you can even do "class Settings { static shared = new Settings() /* .. */ }" so it's obvious for any reader that Settings is a singleton, even without looking into the implementation of the Settings class.

nueno
Автор

Great video! Keep 'em comming, Dom!

adampielach
Автор

That was pretty interesting, not something I had ever considered doing before

smoothbeak
Автор

Very nice solution! Thanks for sharing.

DusanResin
Автор

Thanks! I'm going to look more into design patterns 👍

mattcroat
Автор

Nice one but looks a bit like a solution looking for a problem.
From my (PHP) background this looks like a factory pattern which produces a new class instance. Static methods in the factory will do just that...🙂 And IMHO that's the real use case

montebont
Автор

So what if two users are using the same website, would they be sharing the same values in the constructor?

bradical
Автор

Sir, please explain why did you say chance of one in four thousand nearly at the end of the tutorial, for getting the same objects.

chamnil
Автор

Nice Video, need more videos like this want to understand how to select best structure and design pattern for your application

munafhajir
Автор

Whats the point of using a class for this? Cant I just use a Settings object that way I dont have to create an instance?

SoldierXfreedom
Автор

thanks a lot for your videos on OOP with JS classes <3

ginnerzapata
Автор

Hi, thanks for this tutorial.
Here's a question. In my case I load another html file, and then going from href to another html file.
On that file, the instance is no longer defined for some reason, can you think of a reason why that is?


Thanks, Shefy

onthefly--
Автор

What this a theme? It's pretty!
Thanks for video.

matheusicaro
Автор

Using VS code to develop node.js.

I'm getting error "Property 'instance' does not exist on type 'typeof Settings'."

rainsaarmae
Автор

Keyboard is too loud. Good video though

taylorgraham