From the course: JavaScript: Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Singleton pattern

Singleton pattern - JavaScript Tutorial

From the course: JavaScript: Patterns

Start my 1-month free trial

Singleton pattern

- [Instructor] The singleton pattern can be intimidating when you first hear about it. But you'll see how simple it is, once you understand, that it's simply preventing our class from creating more than one instance of the blue print we've defined. In other words, we use the same principles we've used since the beginning of this chapter, except, we allow only one instance of the class to be created. So, let me demonstrate with some code. So, let's go back to our index.js and what we're going to do first is create a variable, where we'll pass a null variable. So let's go ahead and let instance first null. So basically, when we first start our creation of the instance, it's null. So, that means it's going to allow the class to create a new object. So we're going to make a few changes in a class car. This is all the same, except what we're going to do is insert some if and then else. So, first, we're going to do a if,…

Contents