Answer the question
In order to leave comments, you need to log in
Singleton or normal object?
Now I’m reading about different patterns and I didn’t understand the moment about the singleton a little.
By definition, this is a class that can only have one instance within an application.
And what in such a situation prevents you from simply creating an object that will have similar functionality and that's it? (instead of creating a class and an instance of that class)
What are the benefits of creating a class exactly?
Answer the question
In order to leave comments, you need to log in
And what in such a situation prevents you from simply creating an object
export const singleton = {name: "foo"}; // Полноценный синглтон для JS
What are the benefits of creating a class here?
instanceof
to make sure that it is an instance of that class that can only be in one instance, and not some other object. And if it's "just an object" - you can slip a complete "copy" without any problems by creating it using a literal notation.
IMHO, why is a singleton, and not a regular object - in order for the object to be the same in different parts of your program - you need to pass it there.
Singleton, on the other hand, is enough just to create an instance of this class and the instance created earlier, at the other end of the application, will automatically be pulled up. Which will give us "by definition the only instance" as written by previous speakers.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question