One is that it makes inheritance difficult and the other is that functions such as toString are redefined for each of the new objects created using the Car constructor. This isn't very optimal as the function should ideally be shared between all of the instances of the Car type.
Names inside the object may be either strings or identifiers that are followed by a colon.
Outside of an object, new members may be added to it using assignment as follows myModule. Using object literals can assist in encapsulating and organizing your code and Rebecca Murphey has previously written about this topic in depth should you wish to read into object literals further. That said, if we're opting for this technique, we may be equally as interested in the Module pattern. It still uses object literals but only as the return value from a scoping function.
The Module pattern was originally defined as a way to provide both private and public encapsulation for classes in conventional software engineering. What this results in is a reduction in the likelihood of our function names conflicting with other functions defined in additional scripts on the page.
The Module pattern encapsulates "privacy", state and organization using closures. It provides a way of wrapping a mix of public and private methods and variables, protecting pieces from leaking into the global scope and accidentally colliding with another developer's interface. With this pattern, only a public API is returned, keeping everything else within the closure private.
This gives us a clean solution for shielding logic doing the heavy lifting whilst only exposing an interface we wish other parts of our application to use. The pattern utilizes an immediately-invoked function expression IIFE - see the section on namespacing patterns for more on this where an object is returned.
Within the Module pattern, variables or methods declared are only available inside the module itself thanks to closure. Variables or methods defined within the returning object however are available to everyone. From a historical perspective, the Module pattern was originally developed by a number of people including Richard Cornford in It was later popularized by Douglas Crockford in his lectures.
Another piece of trivia is that if you've ever played with Yahoo's YUI library, some of its features may appear quite familiar and the reason for this is that the Module pattern was a strong influence for YUI when creating their components. Let's begin looking at an implementation of the Module pattern by creating a module which is self-contained. Here, other parts of the code are unable to directly read the value of our incrementCounter or resetCounter.
The counter variable is actually fully shielded from our global scope so it acts just like a private variable would - its existence is limited to within the module's closure so that the only code able to access its scope are our two functions. Our methods are effectively namespaced so in the test section of our code, we need to prefix any calls with the name of the module e.
When working with the Module pattern, we may find it useful to define a simple template that we use for getting started with it. Here's one that covers namespacing, public and private variables:. Looking at another example, below we can see a shopping basket implemented using this pattern. The module itself is completely self-contained in a global variable called basketModule.
The basket array in the module is kept private and so other parts of our application are unable to directly read it. It only exists with the module's closure and so the only methods able to access it are those with access to its scope i. Inside the module, you may have noticed that we return an object. This gets automatically assigned to basketModule so that we can interact with it as follows:.
Notice how the scoping function in the above basket module is wrapped around all of our functions, which we then call and immediately store the return value of. This has a number of advantages including:. This variation of the pattern demonstrates how globals e. This effectively allows us to import them and locally alias them as we wish.
This next variation allows us to declare globals without consuming them and could similarly support the concept of global imports seen in the last example. Dojo Dojo provides a convenience method for working with objects called dojo. This takes as its first argument a dot-separated string such as myObj. Using setObject allows us to set the value of children, creating any of the intermediate objects in the rest of the path passed if they don't already exist. For example, if we wanted to declare basket. For more information on dojo. For those using Sencha's ExtJS, an example demonstrating how to correctly use the Module pattern with the framework can be found below.
Secondly, it supports private data - so, in the Module pattern, public parts of our code are able to touch the private parts, however the outside world is unable to touch the class's private parts no laughing! Oh, and thanks to David Engfer for the joke. The disadvantages of the Module pattern are that as we access both public and private members differently, when we wish to change visibility, we actually have to make changes to each place the member was used.
We also can't access private members in methods that are added to the object at a later point. That said, in many cases the Module pattern is still quite useful and when used correctly, certainly has the potential to improve the structure of our application. Other disadvantages include the inability to create automated unit tests for private members and additional complexity when bugs require hot fixes.
It's simply not possible to patch privates. Instead, one must override all public methods which interact with the buggy privates. Developers can't easily extend privates either, so it's worth remembering privates are not as flexible as they may initially appear. For further reading on the Module pattern, see Ben Cherry's excellent in-depth article on it.
The Revealing Module pattern came about as Heilmann was frustrated with the fact that he had to repeat the name of the main object when we wanted to call one public method from another or access public variables. The result of his efforts was an updated pattern where we would simply define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public. The pattern can also be used to reveal private functions and properties with a more specific naming scheme if we would prefer:.
This pattern allows the syntax of our scripts to be more consistent. It also makes it more clear at the end of the module which of our functions and variables may be accessed publicly which eases readability. A disadvantage of this pattern is that if a private function refers to a public function, that public function can't be overridden if a patch is necessary. This is because the private function will continue to refer to the private implementation and the pattern doesn't apply to public members, only to functions. Public object members which refer to private variables are also subject to the no-patch rule notes above.
As a result of this, modules created with the Revealing Module pattern may be more fragile than those created with the original Module pattern, so care should be taken during usage. The Singleton pattern is thus known because it restricts instantiation of a class to a single object. Classically, the Singleton pattern can be implemented by creating a class with a method that creates a new instance of the class if one doesn't exist.
In the event of an instance already existing, it simply returns a reference to that object. 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.