简体   繁体   中英

Javascript literal object and singleton pattern

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM