Module pattern
In the previous chapter, we had a look at how we can create private variables and namespaces inside functions. We also explored how private scopes can be implemented. Some concepts related to private scopes can also be applied to singleton objects residing inside functions.
A singleton object is an object there will only ever be one instance of it in the application. Singleton objects can be created using object literal notation, which we saw examples of in the previous chapter.
Consider the following object definition:
var mySingletonObj = {};
While the preceding object does not do anything, it is in fact a valid object and there can only be one instance of this object as we cannot create other objects based on mySingletonObj
.
Let's add some value properties and methods (method properties) to this object and see how we can access such properties outside of the object definition:
var mySingletonObj = { name: "Sasan", title: "Software Developer"...