Singleton:
export const mySingleton = {};
Your example is a port from another language like Java, and it is verbose. In addition you can instantiate the class Foo in your example, making it not really a singleton.
Finally, the factory pattern is common in JavaScript. Any function that returns a new object without using the
new
keyword to instantiate it is a factory function.
I don’t think that is correct. Patterns have a purpose, regarding factory patterns described in the book you quote, there are 2 types:
Factory Method Pattern
A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class. The Factory Method Pattern is also known as Virtual Constructor.
Abstract Factory Pattern
Abstract Factory Pattern says that just define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes.That means Abstract Factory lets a class returns a factory of classes. So, this is the reason that Abstract Factory Pattern is one level higher than the Factory Pattern.
So, they both have a purpose, and they are both related to inheritance.
The examples you provide are more related to another pattern: Builder pattern.
Builder Pattern says that “construct a complex object from simple objects using step-by-step approach”.
It is common in JS to provide functions instead of class constructors to provide users with a simpler and more friendly way of creating objects.