JS Interview - Singleton - Question 6

preview_player
Показать описание
This episode answers the question how to create a singleton in JavaScript.

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

Hi Steve,

Good Evening, I hope you and your family are in good health.
Your channel is probably one of the most underrated channel. Your content is very good and explanations are on point. Helped me a lot in preparation for my interviews.
Keep up the good work.

sudiptasaha
Автор

Great video! I'm not sure if you're aware, but as of ES6, there is what's known as the object property value shorthand, when the name of the property and value are the same in an object, you don't really have to do { start: start, stop: stop } and instead simply do { start, stop }. But hey, if you're just avoiding it to keep it beginner friendly, don't mind me!
You can also implement the singleton pattern using ES6 Classes.
Anyways, really love this series! Grabbed a few things the past few videos, really appreciate what you're doing

sheneshperera
Автор

Hi @Steve,
I could relate that closure is being created when we return getInstance as a function but how about create method, it's not using any of the outer scope variable defined in its lexical scope.
Thanks,
Saquib

saquibakhter
Автор

How would you create this singleton from another file? A simple export & import then calling getInstance doesn't work. I get an error that getInstance is not a function.

SethEden
Автор

Your content is very good! I have one question, maybe someone can help me; the second time "obj" arrow function is called, the code inside is no longer the same? "obj" arrow function's first line will be modified to returns values:
"let objInstance = {
start,
stop,
currentState,
}"

is that true?

unaiiglesias
Автор

Great One.
if i write like that
class SingleTone{
constructor(){
if(SingleTone.instance instanceof SingleTone){
return SingleTone.instance;
}
SingleTone.instance=this;
}
}

let sobj = new SingleTone();
let sobj1 = new SingleTone();

the both objects is pointing to the same instance.is it right?

surajankita
Автор

What is the purpose of this method? You could just assign a variable to an object, and then, when needed, assign another variables to the first variable. Every subsequent variable would point to the same object...

maksymantoshkin
Автор

well that was a very messy design pattern. What if we have another constructor/class and we need it's singleton instance? implement another singleton routine again for it? no way. putting all singletones on the self (window, global etc.) object and naming them even gonna works for us.

biliyonnet
Автор

this is so wrong! why don't you just create a single object and export it ?
Singleton are usually used with classes in languages like c++ etc.. to ensure the that the system is using only one instance of that class
you don't really need Singleton on your example

ghostkee