Download e-book Pro JavaScript Design Patterns (Recipes: a Problem-Solution Ap)

Free download. Book file PDF easily for everyone and every device. You can download and read online Pro JavaScript Design Patterns (Recipes: a Problem-Solution Ap) file PDF Book only if you are registered here. And also you can download or read online all Book PDF file that related with Pro JavaScript Design Patterns (Recipes: a Problem-Solution Ap) book. Happy reading Pro JavaScript Design Patterns (Recipes: a Problem-Solution Ap) Bookeveryone. Download file Free Book PDF Pro JavaScript Design Patterns (Recipes: a Problem-Solution Ap) at Complete PDF Library. This Book have some digital formats such us :paperbook, ebook, kindle, epub, fb2 and another formats. Here is The CompletePDF Book Library. It's free to register here to get Book file PDF Pro JavaScript Design Patterns (Recipes: a Problem-Solution Ap) Pocket Guide.

What did you gain from this? Your code is now more loosely coupled because you aren t relying on instances of any particular class. Instead, you are ensuring that the features you require are in place; any concrete implementation can be used, giving you more freedom to optimize and refactor your code. Example: Using the Interface Class Imagine that you have created a class to take some automated test results and format them for viewing on a web page.

This class s constructor takes an instance of the TestResult class as an argument. It then formats the data encapsulated in the TestResult object and outputs it on request. This allows you to code the renderresults method knowing confidently that the getdate and getresults methods will be available to you. Or does it? In the constructor, you are only checking that the resultsobject is an instance of TestResult.

That does not actually ensure that the methods you need are implemented. TestResult could be changed so that it no longer has a getdate method. The check in the constructor would pass, but the renderresults method would fail. The check in the constructor is also unnecessarily limiting. It prevents instances of other classes from being used as arguments, even if they would work perfectly fine. Say, for example, you have a class named WeatherData. It has a getdate and a getresults method and could be used in the ResultFormatter class without a problem. But using explicit type checking with the instanceof operator would prevent any instances of WeatherData from being used.

The solution is to remove the instanceof check and replace it with an interface. The first argument is the name of the interface, and the second is an array of strings, where each string is the name of a required method. The constructor, on the other hand, has been modified to use ensureimplements instead of instanceof. You could now use an instance of WeatherData in this constructor, or any other class that implements the needed methods. By changing a few lines of code within the ResultFormatter class, you have made the check more accurate by ensuring the required methods have been implemented and more permissive by allowing any object to be used that matches the interface.

In order to ensure that the objects created can be used interchangeably, interfaces are used. This means that a factory is guaranteed to produce an object that will implement the needed methods. The composite pattern: You really can t use this pattern without an interface. The most important idea behind the composite is that groups of objects can be treated the same as the constituent objects. This is accomplished by implementing the same interface.

Without some form of duck typing or type checking, the composite loses much of its power. The decorator pattern: A decorator works by transparently wrapping another object. This is accomplished by implementing the exact same interface as the other object; from the outside, the decorator and the object it wraps look identical.

We use the Interface class to ensure that any decorator objects created implement the needed methods. The command pattern: All command objects within your code will implement the same methods which are usually named execute, run, or undo.

  1. Table of Contents.
  2. Preparation of nitrobenzene.
  3. Polymorphism.
  4. Reward Yourself!
  5. Key papers in the development of coding theory!
  6. See a Problem?;
  7. A Real Guide to Really Getting It Together Once and for All:.

By using interfaces, you can create classes that can execute these commands without needing to know anything about them, other than the fact that they implement the correct interface. This allows you to create extremely modular and loosely coupled user interfaces and APIs. The interface is an important concept that we use throughout this book. It s worth playing around with interfaces to see if your specific situation warrants their use.

Summary In this chapter, we explored the way that interfaces are used and implemented in popular object-oriented languages. We showed that all different implementations of the concept of the interface share a couple features: a way of specifying what methods to expect, and a way to check that those methods are indeed implemented, with helpful error messages if they are not. We are able to emulate these features with a combination of documentation in comments , a helper class, and duck typing.

The challenge is in knowing when to use this helper class. Interfaces are not always needed. One of JavaScript s greatest strengths is its flexibility, and enforcing strict type checking where it is not needed reduces this flexibility. But careful use of the Interface class can create more robust classes and more stable code. Declaring a method or attribute as private allows you to shield your implementation details from other objects and promotes a loose coupling between them. It allows you to maintain the integrity of your data and impose constraints on how it can be modified. It also makes your code more reliable and easier to debug in environments where many people are working on the same code base.

In short, encapsulation is a cornerstone of object-oriented design. Despite the fact that JavaScript is an object-oriented language, it does not have any builtin mechanisms for declaring members to be public or private. As in the previous chapter on interfaces, we will create our own way to implement this feature. There are several established patterns for creating objects with public, private, and privileged methods, each with its own strengths and weaknesses. The Information Hiding Principle Let s use an example to illustrate the information hiding principle.

Every evening, you receive a report from a coworker outlining the day s revenues. This is a well-defined interface; you request the information, and your coworker finds the raw data, calculates the revenue, and reports back to you. – Re-inventing the Internet to be data first.

If either you or your coworker moves to another company, that interface will remain, ensuring that it is easy for your replacement to request information the same way. One day you decide that you want to receive this information more frequently than your coworker is willing to give it to you. You find out where the raw data is stored, retrieve it yourself, and perform the calculations. Everything works fine until the format of the data changes. Instead of a file of comma-separated values, it is now formatted in XML. Also, the calculations can change depending on accounting and tax laws, which you have no expertise in.

If you quit, you must first train your replacement to perform these same tasks, which are much more complex than just requesting the end calculation from your coworker. You have become dependent on the internal implementation; when that implementation changes, you must relearn the entire system and start again. In object-oriented design terms, you have become tightly coupled to the raw data. The information hiding principle serves to It states that all information between two actors should be obtained through well-defined channels. In this case, these channels are the interfaces of your objects.

Encapsulation vs. Information Hiding How is encapsulation related to information hiding? You can think of it as two ways of referring to the same idea. Information hiding is the goal, and encapsulation is the technique you use to accomplish that goal. This chapter deals mainly with concrete examples of encapsulation in JavaScript. Encapsulation can be defined as the hiding of internal data representation and implementation details in an object.

The only way to access the data within an encapsulated object is to use defined operations. By using encapsulation, you are enforcing information hiding. Many object-oriented languages use keywords to specify that methods and attributes should be hidden. In Java, for instance, adding the private keyword to a method will ensure that only code within the object can execute it. There is no such keyword in JavaScript; we will instead use the concept of the closure to create methods and attributes that can only be accessed from within the object.

It is more complicated and confusing than just using keywords, but the same end result can be achieved. The Role of the Interface How does the interface help you hide information from other objects? It provides a contract that documents the publicly accessible methods. It defines the relationship that two objects can have; either object in this relationship can be replaced as long as the interface is maintained. It isn t always necessary to use a strict interface, like the one we defined in Chapter 2, but most of the time you will find it very helpful to have the available methods documented.

Even with a known interface in place, it is important to not expose methods that are not defined in that interface. Conversely, it can be dangerous for other objects to rely on methods that are not part of the interface. They may change or be removed at any point, causing the whole system to fail. The ideal software system will define interfaces for all classes.

Those classes will provide only the methods defined in their interfaces; any other method will be kept private. All attributes will be private and only accessible through accessor and mutator operations defined in the interface. Rarely in the real world does a system have all of these characteristics. Good code should aim toward them whenever possible, but not at the cost of complicating a simple project that doesn t really need them.

Basic Patterns In this section we look at examples of the various ways an object can be created and the features available in each. There are three basic patterns that can be used to create objects. The fully exposed object is the simplest but provides only public members. The next pattern improves upon this by using underscores to denote methods and attributes that are intended to be private. The third basic pattern uses closures to create true private members, which can only be accessed through the use of privileged methods.

Depending on your needs, any one of them may suit you. We will use the Book class as our example. You are given this assignment: create a class to store data about a book, and implement a method for displaying the book s data in HTML. You will only be creating the class; other programmers will be instantiating it. Tolkien' ; thehobbit. Fully Exposed Object The easiest way to implement Book is to create a class in the conventional way, using a function as a constructor. We call this the fully exposed object because all of the class s attributes and methods are public and accessible.

Without this, you can t fetch the image or provide a link to buy the book. Because of this, an error is thrown in the constructor if an ISBN is not given. The title and author attributes are both optional, so you provide defaults if they are not given. The Boolean OR operator,, can be used here to provide fallback values. If a title or author is given, the left side will evaluate to true and will be returned. If a title or author is not given, the left side of the operator will evaluate to false, and the right side will be returned instead.

At first glance, this class seems to meet every need. The biggest outstanding problem is that you can t verify the integrity of the ISBN data, which may cause your display method to fail. This breaks the contract you have with the other programmers. If the Book object doesn t throw any errors, the display method should work, but without integrity checks, it won t. Since there are now two methods for this class, Book. Both ways of defining methods are identical, and we use both interchangeably throughout the chapter. This seems to be an improvement.

You are now able to verify that the ISBN is valid when the object is created, thus ensuring that the display method will succeed. However, a problem comes up. Another programmer notices that a book may have multiple editions, each with its own ISBN. He creates an algorithm for selecting among these different editions, and is using it to change the isbn attribute directly after instantiating the object: thehobbit.

In order to protect the internal data, you create accessor and mutator methods for each attribute. An accessor method usually named in the form getattributename will get the value of any of the attributes. A mutator method usually named in the form setattributename will set the value of the attribute. Using mutators, you can implement any kind of verification you like before you actually assign a new value to any of your attributes.

From now on, other programmers should only interact with the object using those methods defined in the interface. Also, the mutator methods are used in the constructor; there is no point implementing the same verifications twice, so you rely on those methods internally. This is as good as it gets with the fully exposed object pattern.

You have a well-defined interface, accessor and mutator methods protecting the data, and validation methods. Despite having all of these features, there is still a hole in the design. Even though we provide mutator methods for setting attributes, the attributes are still public, and can still be set directly. With this pattern, there is no way of preventing that. It is possible to set an invalid ISBN, either accidentally by a programmer who doesn t know he s not supposed to set it directly or intentionally by a programmer who knows the interface but ignores it.

Despite that single flaw, this pattern still holds a lot of benefits. It s easy to use and easy for new JavaScript programmers to pick up quickly. It isn t necessary to have a deep understanding of scope or the call chain in order to create a class like this.

Subclassing is very easy, as is unit testing, since all methods and attributes are publicly available. The only drawbacks are the fact that you cannot protect the internal data, and accessor and mutator methods add extra code that isn t strictly needed. This could be a concern in situations where JavaScript file size is important. Private Methods Using a Naming Convention Next we will take a look at a pattern that emulates private members by using a naming convention.

This pattern addresses one of the problems encountered in the previous section: the inability to prevent another programmer from accidentally bypassing all of your validations. An underscore is added to the beginning of each, signifying that it is intended to be private. This is still a valid variable name in JavaScript, since the underscore is a legal first character in an identifier.

This naming convention can be applied to methods as well. Let s say that a programmer using your class is having a hard time creating an instance because he keeps getting Invalid ISBN errors. He could use the public method checkisbn to run through each possible character for the checksum digit there are only ten until he finds one that passes, and use that to create an instance of Book. You should prevent that sort of behavior because it is likely that the ISBN created will still be invalid. Using an underscore is a well-known naming convention; it says that the attribute or method is used internally, and that accessing it or setting it directly may have unintended consequences.

It should prevent programmers from setting it in ignorance, but it still won t prevent those that use it knowingly. For that, you need real private methods. This pattern has all of the benefits of a fully exposed object, and one less drawback. It is, however, a convention that must be agreed upon to have any real use.

No enforcement is possible, and as such, it is not a real solution for hiding the internal data of an object. It is instead used mostly for methods and attributes that are internal but not sensitive methods and attributes that most programmers using the class won t care about since they aren t in the public interface. Scope, Nested Functions, and Closures Before we get into real private methods and attributes, we should take a moment to explain the theory behind the technique we will use. In JavaScript, only functions have scope; that is to say, a variable declared within a function is not accessible outside of that function.

Private attributes are essentially variables that you would like to be inaccessible from outside of the object, so it makes sense to look to this concept of scope to achieve that inaccessibility. A variable defined within a function is accessible to its nested functions. When bar is executed, it sets a to a times 2. It makes sense that bar can access a when it is executed within foo, but what if you could execute bar outside of foo?

Here a reference to the function bar is returned and assigned to the variable baz. This function is now executed outside of foo, and it still has access to a. This is possible because JavaScript is lexically scoped. Functions run in the scope they are defined in in this case, the scope within foo , rather than the scope they are executed in. As long as bar is defined within foo, it has access to all of foo s variables, even if foo is finished executing.

This is an example of a closure. After foo returns, its scope is saved, and only the function that it returns has access to it. In the previous example, baz and blat each have a copy of this scope and a copy of a that only they can modify. The most common way of creating a closure is by returning a nested function.

Private Members Through Closures Back to the problem at hand: you need to create a variable that can only be accessed internally. A closure seems to be a perfect fit because it allows you to create variables that are accessible only to certain functions and are preserved in between those function calls. To create private attributes, you define variables in the scope of your constructor function.

In the other Book examples, we always created and referred to the attributes using the this keyword. In this example, we declared these variables using var. That means they will only exist within the Book constructor. We also declare the checkisbn function in the same way, making it a private method. Any method that needs to access these variables and functions need only be declared within Book. These are called privileged methods because they are public but have access to private attributes and methods.

The this keyword is used in front of these privileged functions to make them publicly accessible. Because these methods are defined within the Book constructor s scope, they can access the private attributes. They are not referred to using this because they aren t public. All of the accessor and mutator methods have been changed to refer to the attributes directly, without this. Any public method that does not need direct access to private attributes can be declared normally in the Book. An example of one of these methods is display; it doesn t need direct access to any of the private attributes because it can just call getisbn or gettitle.

It s a good idea to make a method privileged only if it needs direct access to the private members. Having too many privileged methods can cause memory problems because new copies of all privileged methods are created for each instance. With this pattern, you can create objects that have true private attributes. It is impossible for other programmers to create an instance of Book and directly access any of the data. You can tightly control what gets set because they are forced to go through the mutator methods.

This pattern solves all of the problems with the other patterns, but it introduces a few drawbacks of its own. In the fully exposed object pattern, all methods are created off of the prototype, which means there is only one copy of each in memory, no matter how many instances you create. In this pattern, you create a new copy of every private and privileged method each time a new. This has the potential to use more memory than the other patterns, so it should only be used when you require true private members. This pattern is also hard to subclass.

The new inherited class will not have access to any of the superclass s private attributes or methods. It is said that inheritance breaks encapsulation because in most languages, the subclass has access to all of the private attributes and methods of the superclass. In JavaScript, this is not the case. If you are creating a class that might be subclassed later, it is best to stick to one of the fully exposed patterns. More Advanced Patterns Now that you have three basic patterns at your disposal, we ll show you a few advanced patterns.

Part 2 of this book goes into much more detail about specific patterns, but we will take an introductory look at a few of them here. Static Methods and Attributes Applying the lesson of scope and closures from earlier in the chapter can lead to a way to create static members, which can be both private and publicly accessible. Most methods and attributes interact with an instance of a class; static members interact with the class itself. Another way of putting it is to say that static members operate on the class-level instead of the instance-level; there is only one copy of each static member.

As you will see later in this section, static members are called directly off of the class object. Private and privileged members are still declared within the constructor, using var and this respectively, but the constructor is changed from a normal function to a nested function that gets returned to the variable Book. This makes it possible to create a closure where you can declare private static members.

The empty parentheses after the function declaration are extremely important. They serve to execute that function immediately, as soon as the code is loaded not when the Book constructor is called. The result of that execution is another function, which is returned and set to be the Book constructor. When Book is instantiated, this inner function is what gets called; the outer function is used only to create a closure, within which you can put private static members. There is also a static attribute called numofbooks, which allows you to keep track of how many times the Book constructor has been called.

In this example, we use that attribute to limit the constructor to creating only 50 instances. These private static members can be accessed from within the constructor, which means that any private or privileged function has access to them. They have a distinct advantage over these other methods in that they are only stored in memory once. Since they are declared outside of the constructor, they do not have access to any of the private attributes, and as such, are not privileged; private methods can call private static methods, but not the other way around.

A rule of thumb for deciding whether a private method should be static is to see whether it needs to access any of the instance data. If it does not need access, making the method static is more efficient in terms of memory use because only a copy is ever created. Public static members are much easier to create. They are simply created directly off of the constructor, as with the previous method converttotitlecase. This means you are essentially using the constructor as a namespace.

Note In JavaScript, everything except for variables of the three primitive types is an object and even those primitives are automatically wrapped by objects when needed. This means that functions are also objects. Since objects are essentially hash tables, you can add members at any time. The end result of this is that functions can have attributes and methods just like any other object, and they can be added whenever you want.

All public static methods could just as easily be declared as separate functions, but it is useful to bundle related behaviors together in one place. They are useful for tasks that are related to the class as a whole and not to any particular instance of it. They don t directly depend on any of the data contained within the instances. Constants Constants are nothing more than variables that can t be changed. In JavaScript, you can emulate constants by creating a private variable with an accessor but no mutator.

Since constants are usually set at development time and don t change with each instance that is created, it makes sense to create them as private static attributes. The two that rely on it the most are the singleton pattern and the factory pattern. Both are covered in more detail later in the book, but we mention them here because they use these same concepts to hide information.

The singleton pattern uses a returned object literal to expose privileged members, while keeping private members protected in the enclosing function s scope. It uses the same technique that we covered earlier, where an outer function is executed immediately and the result. In the examples so far in this chapter, a function has always been returned; a singleton returns an object literal instead.

It is a very easy and straightforward way to create a sheltered namespace. We talk more about singletons in Chapter 5. Object factories can also use closures to create objects with private members. In its simplest form, an object factory is the same as a class constructor, and all of the patterns we discuss here can be applied to it. The factory pattern is covered in detail in Chapter 7.

Benefits of Using Encapsulation It s true that it would be much simpler to not have to worry about things such as closures and privileged methods when creating an object. In a perfect world, all methods could be public, and other programmers would only use the ones specified in the interface. So what do you gain by going through the trouble of hiding your implementation details?

Encapsulation protects the integrity of the internal data. By allowing access to the data only through accessor and mutator methods, you have complete control over what gets saved and returned. This allows you to reduce the amount of error-checking code you need in your other functions, and ensures that the data can never be in a bad state. It also has the added benefit of allowing easier refactoring of your objects. Since the internal details are shielded from the users of the object, you are free to change data structures and algorithms in midstream without anyone knowing or caring.

By making only the methods specified in the interface public, you are promoting loosely coupled modules. This is one of the most important principles of object-oriented design. Keeping your objects as independent as possible has many benefits. It improves reusability and allows objects to be swapped out if needed. Using private variables also protects you from having to worry about namespace collisions.

By making a variable inaccessible to the rest of the code, you don t have to constantly ask yourself if the variable name you are using might interfere with other objects or functions elsewhere in the program. It allows internal object details to change dramatically without affecting other pieces of code; in general, you can make changes more easily because you already know exactly what it will affect. If you expose internal data directly, it would be impossible to know what consequences code changes could have.

Drawbacks to Using Encapsulation It can be very hard to unit test private methods. Because of the very fact that they are hidden, and their internal variables are shielded, it is impossible to access them outside of the object. The workarounds for this aren t very appealing. You must either provide access through public methods, removing most of the benefit of using private methods in the first place, or somehow define and execute all unit tests within the object.

The best solution to this problem is to only unit test the public methods. This should provide complete coverage of the private methods, though only indirectly. This problem is not specific to JavaScript, and it is generally accepted that you should only unit test your public methods. Having to deal with complicated scope chains can make debugging errors more difficult. This usually isn t a big problem, but there are situations where it can be hard to distinguish between many identically named variables in different scopes.

  • Modernization and Bureaucratic-Authoritarianism: Studies in South American Politics.
  • Ripe for the Picking.
  • The God of the Hive: A novel of suspense featuring Mary Russell and Sherlock Holmes.
  • Sartre and the Problem of Morality (Studies in Phenomenology and Existential Philosophy)!
  • This problem is not unique to encapsulated objects, but it can be made more complicated by the closures needed to produce private methods and attributes. If you don t have a clear understanding of how your classes may be used by other programmers, actively preventing them from modifying the internal details may be too restrictive. It s hard to predict how people will use your code. Encapsulation could make your classes so inflexible that it is impossible to reuse them to achieve a purpose you hadn t anticipated.

    The biggest drawback is that it is hard to implement encapsulation in JavaScript. It requires complicated object patterns, most of which are very unintuitive for novice programmers. Having to understand concepts such as the call chain and immediately executed anonymous functions adds a steep learning curve to a language that is already very different from most other objectoriented languages.

    Furthermore, it can make existing code hard to decipher for someone not well-versed in a particular pattern. Descriptive comments and documentation can reduce this problem a bit, but not eliminate it completely. If you are going to be using these patterns, it is important that the other programmers you work with also understand them. Summary In this chapter we looked at the concept of information hiding and how to enforce it with encapsulation.

    Since JavaScript has no built-in way to do this, you must rely on other techniques. Fully exposed objects are useful when it isn t crucial to maintain the integrity of internal data, or when other programmers can be trusted to use only the methods described in the interface. Naming conventions can also help to steer other programmers away from internal methods that shouldn t be accessed directly. If true private members are needed, the only way to create them is through closures. By creating a protected variable space, you can implement public, private, and privileged members, along with static class members and constants.

    Most of the later chapters in this book rely on these basic techniques, so it is worth going over them carefully. Once you understand how scope can be manipulated in JavaScript, any object-oriented technique can be emulated. Unlike most other OO languages, where a simple keyword will allow you to inherit from a class, JavaScript requires a series of steps in order to pass on public members in the same way. To further complicate the issue, JavaScript is one of the few languages that uses prototypal inheritance we will show you how this is actually a huge benefit.

    Because of the flexibility of the language, you can choose to use standard class-based inheritance, or the slightly trickier but also potentially more efficient prototypal inheritance. In this chapter, we look at the techniques that can be used to create subclasses in JavaScript, and the situations where it would be appropriate to use each. Why Do You Need Inheritance?

    Before we even get into any code, we need to figure out what s to gain by using inheritance. Generally speaking, you want to design your classes in such a way as to reduce the amount of duplicate code and make your objects as loosely coupled as possible. Inheritance helps with the first of those two design principles, and allows you to build upon existing classes and leverage the methods they already have.

    It also allows you to make changes more easily. If you require several classes to each have a tostring method that outputs the structure of the class in a certain way, you could copy and paste a tostring method declaration into each class, but then each time you need to change how the method works, you would have to make the change to each class. If instead you create a ToStringProvider class and make each of the other classes inherit from it, this method would be declared in only one place. There is the possibility that by making one class inherit from another, you are making them strongly coupled.

    That is, one class depends on the internal implementation of another. We will look at different ways to prevent that, including using mixin classes to provide methods to other classes. By convention, this should be the name of the class, starting with a capital letter. Within the constructor, use the this keyword to create instance attributes. To create methods, add them to the class s prototype object, as in Person. This is a very simple example of a class in JavaScript. First, create a constructor function, as in the previous example. Within that constructor, call the superclass s constructor, and pass in the name argument.

    This line deserves a little more explanation. When you use the new operator, several things are done for you. The first is that an empty object is created. The constructor function is then called with this empty object at the front of the scope chain; the this in each. So to call the superclass s constructor within Author, you must do the same thing manually. The next step is to set up the prototype chain. Despite the fact that the code used to do this is fairly simple, it is actually a very complex topic.

    As mentioned before, JavaScript has no extends keyword; instead, every object has an attribute named prototype. This attribute points to either another object or to null. When a member of an object is accessed as in reader. If it is not found there, it will continue up the chain, accessing each objects prototype until the member is found or until the prototype is null.

    This means that in order to make one class inherit from another, you simply need to set the subclasses s prototype to point to an instance of the superclass. This is completely different from how inheritance works in other languages and can be very confusing and counterintuitive. In order to have instances of Author inherit from Person, you must manually set Author s prototype to be an instance of Person.

    The final step is to set the constructor attribute back to Author when you set the prototype attribute to an instance of Person, the constructor attribute is wiped out. Creating new instances is still simple. The extend Function In order to make the class declaration more simple, you can wrap the whole subclassing process in a function, called extend.

    It sets the prototype and then resets the correct constructor. As a bonus, it adds the empty class F into the prototype chain in order to prevent a new and possible large instance of the superclass from. This is also beneficial in situations where the superclass s constructor has side effects or does something that is computationally intensive. Since the object that gets instantiated for the prototype is usually just a throwaway instance, you don t want to create it unnecessarily. The only problem with this is that the name of the superclass Person is hardcoded within the Author declaration.

    The first four lines of the function are the. The last three lines simply ensure that the constructor attribute is set correctly on the superclass even if the superclass is the Object class itself. This is useful if you want to override a method while still having access to the superclass s implementation of it. For instance, to override Person s implementation of getname with a new version, you could use Author.

    We ve found the best way to think about it is to forget everything you know about classes and instances, and think only in terms of objects. The classical approach to creating an object is to a define the structure of the object, using a class declaration, and b instantiate that class to create a new object. Objects created in this manner have their own copies of all instance attributes, plus a link to the single copy of each of the instance methods. In prototypal inheritance, instead of defining the structure through a class, you simply create an object.

    This object then gets reused by new objects, thanks to the way that prototype chain lookups work. It is called the prototype object because it provides a prototype for what the other objects should look like in order to prevent confusion with the other prototype object, it will appear in italics. It is where prototypal inheritance gets its name. It is the prototype object for any other Person-like objects that you want to create. Define all attributes and methods you want these objects to have, and give them default values. To create a new Person-like object, use the clone function we go into more detail about this function later in the section The clone Function.

    This provides an empty object with the prototype attribute set to the prototype object. This means that if any method or attribute lookup on this object fails, that lookup will instead look to the prototype object. To create Author, you don t make a subclass of Person. You can change the default values given by Person, or you can add new attributes and methods.

    Here is one example of that. In classical inheritance, each instance of Author has its own copy of the books array. You could add to it by writing author[1]. That is not initially possible with the object you created using prototypal inheritance because of the way prototype chaining works. A clone is not a fully independent copy of its prototype object; it is a new empty object with its prototype. When it is just created, author[1]. This is because of the asymmetry inherent in reading and writing objects linked from the prototype.

    When you read the value of author[1]. When you write to author[1]. This also illustrates why you must create new copies of data types that are passed by reference. In the previous example, pushing a new value onto the authorclone. This is bad because you just modified the value not only for Author but for any object inheriting from Author that has not yet overwritten the default. You must create new copies of all arrays and objects before you start changing their members.

    It is very easy to forget this and modify the value of the prototype object. This should be avoided at all costs; debugging these types of errors can be very time-consuming. In these situations, you can use the hasownproperty method to distinguish between inherited members and the object s actual members. Sometimes prototype objects will have child objects within them. If you want to override a single value within that child object, you have to recreate the entire thing.

    This can be done by setting the child object to be an empty object literal and then recreating it, but that would mean that the cloned object would have to know the exact structure and defaults for each child object. Changes the value of CompoundObject. The problem is that compoundobjectclone must know that childobject has two attributes, with values true and It then sets the prototype attribute of F to the prototype object. You can see here the intent of the original JavaScript creators. The prototype attribute is meant to point to the prototype object, and through prototype.

    Lastly, the function creates a new object by calling the new operator on F. The cloned object that is returned is completely empty, except for the prototype attribute, which is indirectly pointing to the prototype object, by way of the F object. Comparing Classical and Prototypal Inheritance The classical and prototypal paradigms for creating new objects are very different from each other, and the objects that each one produces behave differently. Each paradigm has its own pros and cons, which should help you determine which one to use in a given situation.

    Classical inheritance is well understood, both in JavaScript and the programmer community in general. Almost all object-oriented code written in JavaScript uses this paradigm. If you are creating an API for widespread use, or if there is the possibility that other programmers not familiar with prototypal inheritance will be working on your code, it is best to go with classical.

    JavaScript is the only popular, widely used language that uses prototypal inheritance, so odds are most people will never have used it before. It can also be confusing to have an object with links back to its prototype object. Programmers who don t fully understand prototypal inheritance will think of this as some sort of reverse inheritance, where the parent inherits from its children. Even though this isn t the case, it can still be a very confusing topic. But since this form of classical inheritance is only imitating true class-based inheritance, advanced JavaScript programmers need to understand how prototypal inheritance truly works at some point anyway.

    Some would argue that hiding this fact does more harm than good. Prototypal inheritance is very memory-efficient.

    Javascript Design Patterns #3 - Strategy Pattern

    Because of the way prototype chain reads members, all cloned objects share a single copy of each attribute and method, until those attributes and methods are written to directly on the cloned object. Contrast this with the objects created using classical inheritance, where each object has a copy of every attribute and private method in memory. The savings here are enormous. It also seems to be a much more elegant approach, needing only a single clone function, rather than several lines of incomprehensible syntax such as SuperClass. - Gokhan Ercan Personal

    Don t think that just because prototypal inheritance is simple that it isn t also complex. Its power lies in its simplicity. The decision to use classical or prototypal inheritance probably depends most on how well you like each paradigm. Some people seem naturally drawn to the simplicity of prototypal inheritance, while others are much more comfortable in the more familiar classical. Both paradigms can be used for each pattern described in this book. We tend toward classical inheritance for the later patterns, to make them easier to understand, but both can be used interchangeably throughout this book.

    Inheritance and Encapsulation Up to this point in the chapter there has been little mention of how encapsulation affects inheritance. When you create a subclass from an existing class, only the public and privileged members are passed on. This is similar to other object-oriented languages. In Java, for instance, no private methods are accessible in subclasses; you have to explicitly define a method to be protected in order to pass it on to the subclasses. All of the members are public and will be passed on to the subclasses. If a member needs to be shielded a bit, the underscore convention can always be used.

    If a class with true private members is subclassed, the privileged methods will be passed on, since they are publicly accessible. These will allow access to the private attributes indirectly, but none of the subclass s instance methods will have direct access to these private attributes.

    Private members can only be accessed through these already established privileged methods; new ones cannot be added in the subclass. Mixin Classes There is a way to reuse code without using strict inheritance. If you have a function that you wish to use in more than one class, you can share it among multiple classes through augmentation. In practice, it goes something like this: you create a class that contains your general-purpose methods, and then use it to augment other classes. These classes with the general-purpose methods are called mixin classes.

    They are usually not instantiated or called directly. They exist only to pass on their methods to other classes. This method walks through each member in this and outputs it as a string. This is only a simple example; a more robust version of this sort of function can be found in the tojsonstring method, part of Douglas Crockford s JSON library at This sort of method could potentially be useful in many different types of classes, but it doesn t make sense to have each of these classes inherit from Mixin. Similarly, duplicating the code in each class doesn t make much sense either.

    Instances of Author can now call serialize. This can be thought of as a way to implement multiple inheritance in JavaScript. But a class can be augmented by more than one mixin class, which effectively provides the same functionality. The augment function is fairly simple. Using a for.. If the member already exists, skip it. Let s say you have a mixin class containing several methods but only want to copy one or two of them over to another class. With the version of augment given previously, that would be impossible.

    More method names can be added if you want to augment with more than one method. Often it makes more sense to augment a class with a few methods than it does to make one class inherit from another. This is a lightweight way to prevent code duplication. Unfortunately, there aren t many situations where it can be used. Only methods general enough to be used in very dissimilar classes make good candidates for sharing if the classes aren t that dissimilar, normal inheritance is often a better choice.

    For this example, imagine that you have been given a task: write a modular, reusable API for creating and managing edit-in-place fields edit-in-place refers to a normal block of text in a web page that when clicked turns into a form field and several buttons that allow that block of text to be edited.

    It should allow you to assign a unique ID to the object, give it a default value, and specify where in the page you want it to go. It should also let you access the current value of the field at any time and have a couple of different options for the type of editing field used e. It has a couple of configuration methods createelements, attachevents , a few internal methods for converting and saving converttoeditable, save, cancel, converttotext , and an accessor and mutator pair getvalue, setvalue.

    If this were to be used as production code, it would be a good idea to give each of the HTML elements specific class names so that they can be styled with CSS; for the sake of simplicity, we don t include these lines of code. Next, create a class that will use a text area instead of a text input. This new class uses a text area instead of a text input, and a paragraph tag instead of a span.

    Classical inheritance seems like an ideal technique to use in this case.

    ISBN 10: 159059908X

    Subclassing the EditInPlaceField class is trivial, requiring only a few lines of code. Making changes to the class is as simple as overriding or adding methods on the prototype. We could link the field to another output by creating another subclass and overriding the save method. Since the changes between classes are small, strict inheritance like this is ideal. Prototypal inheritance doesn t use constructors, so you move that code into a configure method instead. Other than that, the code is almost identical to the first example. Then configure that copy. At this point you can interact with the object titleprototypal in the same way as you would with the previous titleclassical object.

    The two objects are almost indistinguishable and can be managed using the same API. This prototype object can be used and cloned in the same way as the first one can. In fact, new prototype objects can be created in the same way, by cloning this one and making a few changes. Prototypal inheritance also seems ideal for this example, for the same reasons that classical inheritance worked so well. Most of the code including all of the methods is completely unchanged.

    This illustrates how easily you can convert from one paradigm to the other. It isn t always this easy, especially with classes and objects that make extensive use of arrays or objects as members, but for the most part you need only modify a bit of the syntax. Using prototypal inheritance in this example doesn t really provide anything over classical inheritance.

    The objects do not use many default values, so you aren t really saving any memory. Personally, we would have a hard time picking one paradigm over the other in this example; both work equally well. We will create one mixin class with all of the methods we want to share. To create the class that uses a text area field, you will not subclass EditInPlaceField.

    Instead, simply create a new class with a constructor and augment it from the same mixin class. But before augmenting it, define a few methods. In the end, the objects created by each of the techniques are almost identical, but from an organizational standpoint, strict inheritance makes more sense than augmentation. Mixin classes work well for methods that are shared between several disparate classes, but in this example, the mixin class is used to provide all of the methods, for two very similar classes.

    Code maintenance would be easier with the first two examples because it is immediately obvious where the methods came from and how the classes and objects were organized. Sharing general-purpose methods that can act on all types of objects is a much better use of mixin classes. Some examples of this are methods that serialize an object to a string representation, or output its state for debugging. It is also possible to use mixin classes to emulate enumerations or iterators, as found in some other object-oriented languages. When Should Inheritance Be Used?

    Inheritance adds some complexity to your code and makes it harder for JavaScript novices to understand what it does, so it should only be used in situations where its benefits outweigh these drawbacks. Most of the benefits have to do with code reuse. By having classes or objects inherit from each other, you only have to define a given method once.

    By the same token, if you ever have to make changes to this method or track down errors in it, the fact that it is defined in a single location can save you a great deal of time and effort. Each paradigm also has its own pros and cons. Prototypal inheritance with the clone function is best used in situations where memory efficiency is important. Classical inheritance with the extend function is best used when the programmers dealing with the objects are familiar with how inheritance works in other object-oriented languages.

    Both of these methods are wellsuited to class hierarchies where the differences between each class are slight. If the classes are very different from each other, it usually makes more sense to augment them with methods from mixin classes. You will find that simpler JavaScript programs rarely require this level of abstraction.

    It is only with large projects, with multiple programmers involved, that this sort of organization becomes necessary. Dec 25, James Stewart rated it really liked it Recommends it for: experienced web developers. Shelves: techbooks. Design patterns, and particularly their application in dynamic languages can be a controversial topic, and every now and again another round of blog posts bubbles up appalled at the way a new group of programmers have become infatuated with design patterns.

    Applied without care design patterns can quickly lead to over-engineered code that seems designed as much to draw on as many of the established patterns as possible as to solve the intended problem. But if applied with care, and with consider Design patterns, and particularly their application in dynamic languages can be a controversial topic, and every now and again another round of blog posts bubbles up appalled at the way a new group of programmers have become infatuated with design patterns. But if applied with care, and with consideration of how a pattern applies in the context of your chosen language they can be a helpful way to draw on the wisdom of the coders that came before you, and make your code easier to understand to those who may inherit it.

    As the book goes on the usefulness of those interfaces, particularly for large development teams, becomes clear and most of those concerns are allayed, especially as the authors offer pros and cons for the use of each pattern and are clearly focussed on how these patterns can help produce more robust solutions. Most of the patterns will have a fairly immediate impact for developers new to them, and even for those who have used them in other contexts it is helpful to see how they have been applied in JavaScript.

    Most modern JavaScript libraries rely on several of these patterns to abstract out handling of different browser quirks or adding new event types, and even if you rely heavily on one or more of the major libraries this guide may well help you understand their internals better. But in this case it seems deserved. But if you have some serious JavaScript development experience and are needing a way to tighten up your code to make it more modular and more maintainable, this book is well worth your time. Disclaimer: I was sent a copy of this book for review by the publisher.

    Feb 27, Costin Manda rated it liked it Shelves: owned. The book started really nice, at a beginner to medium level with which I could not feel neither embarrassed nor overwhelmed. The first chapter was about the expressiveness of Javascript and how different styles of programming could be employed to achieve the same goals. This part of it I would have liked to see expanded in a book of its own, with code examples and everything.

    The second chapter was also interesting, comparing the interface style of programming with the options available inside J The book started really nice, at a beginner to medium level with which I could not feel neither embarrassed nor overwhelmed. The second chapter was also interesting, comparing the interface style of programming with the options available inside Javascript as well as giving some real life solutions. The third chapter, Encapsulation and Information Hiding, described object creation, private, privileged not protected! All these are great reading for a Javascript programmer, as they might teach one or two new things.

    From then on, 13 chapters described various software patterns and their application in Javascript. Alas, since this was the explicit purpose of the book, I can't say I enjoyed that part of the book. It felt like any other rehashing of the original GoF book, only with the syntax changed. Well, maybe not quite so bad, but it lacked a consistency and a touch of the writer's personality that makes books easy to read and to remember. That being said, the technical part was top notch and the structure of each chapter made it easy to understand everything in them.

    Overall, a nice book for reference, but not one that I would call memorable. An easy read and also an easy browse, since one can pass quickly through the book and still understand what it is all about. Aug 13, Dan Kalbasi rated it liked it Shelves: engineering. Some good points of this book: 1. It explains most important design patterns in JS and it comes with comprehensive examples in ES5.

    The author has a good mindset to solve the issues and the mindset itself is inspiring. If you are not too familiar with vanilla JS ES5 , this is a great book to get a good overview of it. Some stuff that is not valid anymore: 1. Some examples such as implementing the in Some good points of this book: 1. Some examples such as implementing the interface manually or using a huge amount of inheritance in the code is not a valid practice anymore. We got other benefits in existing JS frameworks and libraries which are good to mention like A higher-order component technique that React is using.

    Elaborating on new technique is a big part I believe a modern JS book must have, otherwise it may not be practical anymore. Feb 24, Craig Cecil rated it really liked it Shelves: computers. Let me just say this up front—this book contains some advanced JavaScript techniques and code, and is not for the beginning or even intermediate JavaScript programmer. Even advanced practitioners may want to go back through for a second reading to ensure that all the concepts within are fully understood, especially when and how to use these patterns, as well as the advantages and disadvantages of each in context of the JavaScript implementations.

    Here we have two advanced authors, one from Googl Let me just say this up front—this book contains some advanced JavaScript techniques and code, and is not for the beginning or even intermediate JavaScript programmer. Here we have two advanced authors, one from Google Dustin Diaz , the other from Yahoo!

    They present 12 design pattern implementations in JavaScript, with examples of how each could be used, as well as the important core concepts of interfaces, encapsulation, information hiding, inheritance and chaining. Even if you only come away from the book learning and implementing one of the patterns or concepts in your code, it will help make the code you write more scalable, extensible, and longer lasting. Dec 22, Balhau rated it liked it. Very good book on core javascript and oop paradigm. In this approach these two developers give an implementation overview of the main tools and patterns that you use in your everyday when developing with languages as java or other full oop language.

    Since Javascript is not a oop language by design but instead a multi paradigm language you must emulate the main features of oop on top of the core concepts of these lan Very good book on core javascript and oop paradigm. Since Javascript is not a oop language by design but instead a multi paradigm language you must emulate the main features of oop on top of the core concepts of these language. This is where all the magic happens. The language is so versatile that you can, with relatively verbosity, emulate oop on top of those core concepts.

    If you have read the book of Stoyan Stefanov this is just stretching the rope on the same concepts. On a overall analysis i must say that this is an excelent book. Nov 03, David rated it really liked it. Overall I found this book worth the read despite it being a little old. Design Patterns are one of those things where I think you need to see them from several perspectives.

    If this is an area you are interested in, definitely check it out. On the downside, I found some of the examples a little too complicated and could have been made simpler to get the point across. Also, you may find yourself just staring at the code samples for a few minutes because some can be kind of long. On the upside, I Overall I found this book worth the read despite it being a little old. On the upside, I did like how each pattern had a practical example, as opposed to using silly examples with cars.

    If you do get this book, also check out Addy Osmani's design pattern book. Feb 19, Rakesh Gopal rated it it was amazing. Must read for any Javascript developer. After reading this book, you'll never see Javasript the same way again. If you are like me, you'll get an intense urge to rewrite all the Javascript code in your current project and curse yourself for not reading this book long back. We all admire how elegant the JQuery library is. This book takes examples of functions and classes in JQuery, Dojo and YUI and show through examples of how much simpler our life would have been, if all our JS code was written similar to JQuery or Dojo.

    Or better still, using JQuery or Dojo. Feb 07, Ernesto added it Recommends it for: Web Developers. This book has tons of great JS practices, presented in practical examples and for the most part is extremely easy to follow. Heck, if you use JavaScript at all, even if you think you don't need to use "all that Object Oriented stuff" you'll get a lot out of reading this book. View 1 comment. Mar 25, Ivo Stoykov rated it liked it Shelves: computer , programming , software. Close attention is brought to interfaces in conjunction of the known patterns which in Javascript is left to the developers' goodwill.

    Though there is no doubt that interfaces have own place in software development in the case of JavaScipt applications where many independent developers work interfaces could easily become a nightmare. Aug 25, Rick Toews marked it as to-read Shelves: javascript , programming , design-patterns , professional-development. Bridge Pattern. This is where the bridge pattern becomes particularly useful. According to the Gang of Four, the bridge pattern "should decouple an abstraction from its implementation so that the two can vary independently.