5

Isn't the literal object instantiation in javascript a singleton?

Of course there is no "self" instantiation if the object is not found, but there is only one "copy" of the object kept around.

what are your thoughts?

Draconar
  • 1,147
  • 1
  • 17
  • 36
  • 1
    Related: [Simplest/Cleanest way to implement singleton in JavaScript?](http://stackoverflow.com/questions/1479319/simplest-cleanest-way-to-implement-singleton-in-javascript) – Jonathan Lonowski Dec 04 '14 at 16:55

1 Answers1

2

I found the answer in the article:

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#singletonpatternjavascript

Singletons differ from static classes (or objects) as we can delay their initialization, generally because they require some information that may not be available during initialization time. They don't provide a way for code that is unaware of a previous reference to them to easily retrieve them. This is because it is neither the object or "class" that's returned by a Singleton, it's a structure. Think of how closured variables aren't actually closures - the function scope that provides the closure is the closure.

Draconar
  • 1,147
  • 1
  • 17
  • 36
  • 1
    This is an 8 year old thread, but I found it useful! Thank you :). That quote makes so much sense especially it's the "structure" that is returned. – jshji Feb 26 '23 at 11:32