|
|
|
|
|
by mrfabbri
5588 days ago
|
|
The code above doesn't provide a Singleton implementation, you can test it by using the expression mySingleton() == mySingleton() which should evaluate to true, while it evaluates to false. The problem is that the function returns an object literal but every time the function is called it creates a new one (i.e. {} == {} evaluates to false). You can find a sound definition of a Singleton in JavaScript which makes use of the (quite tricky) Lazy Function Definition here: http://stackoverflow.com/questions/1895635/javascript-single... . EDIT: Took a deeper look at the "book" and the singleton section and I think it somewhat misuses the term Singleton (or it uses in a "broad" sense): the example you reported provides some information hiding but no single instantiation restriction, which it is instead provided by the final example/iteration. |
|