First, it is a form of code reuse. If a new class is mostly similar to an existing
class, youonly have to specify the differences.
Secondly, it includes the specification of a system of types. This mostly frees the programmer from having to write explicit casting operations, which is a very good thing because when casting,
the safety benefits of a type system are lost.
JavaScript provides a much richer set of code reuse patterns. It can ape the classical
pattern, but it also supports other patterns that are more expressive. The set of possi-
ble inheritance patterns in JavaScript is vast.
In classical languages, objects are instances of classes, and a class can inherit from
another class. JavaScript is a prototypal language, which means that objects inherit
directly from other objects.
JavaScript is conflicted about its prototypal nature. Its prototype mechanism is obscured by some complicated syntactic business that looks vaguely classical.
Instead of having objects inherit directly from other objects, an unnecessary level of indirection is inserted such that objects are produced by constructor functions.
The pseudoclassical form can provide comfort to programmers who are unfamiliar with JavaScript, but it also hides the true nature of the language.
The classically inspired notation can induce programmers to compose hierarchies that are unneces-
sarily deep and complicated. Much of the complexity of class hierarchies is motivated by the constraints of static type checking.
JavaScript is completely free of those constraints. In classical languages, class inheritance is the only form of code reuse. JavaScript has more and better options.
In a purely prototypal pattern, we dispense with classes. We focus instead on the objects.
Prototypal inheritance is conceptually simpler than classical inheritance: a new object can inherit the properties of an old object.
One weakness of the inheritance patterns we have seen so far is that we get no privacy. All properties of an object are visible. We get no private variables and no private methods. In frustration, some uninformed programmers have adopted a pattern of pretend privacy.
If they have a property that they wish to make private, they give it an odd looking name, with the hope that other users of the code will pretend that they cannot see the odd looking members.
Fortunately, we have a much better alternative in an application of the module pattern.
We start by making a function that will produce objects.
Useful If contstructors take lots of parameters
The my object is a container of secrets that are shared by the constructors in the inheritance chain. The use of the my object is optional. If a my object is not passed in, then a my object is made.
Let’s apply this pattern to our mammal example. We don’t need my here, so we’ll just leave it out, but we will use a spec object.
The name and saying properties are now completely private. They are accessible only via the privileged get_name and says methods:
Creating the cat
The functional pattern also gives us a way to deal with super methods. We will make a superior method that takes a method name and returns a function that invokes that method.
The function will invoke the original method even if the property is changed:
Let’s try it out on a coolcat that is just like cat except it has a cooler get_name method that calls the super method. It requires just a little bit of preparation. We will declare a super_get_name variable and assign it the result of invoking the supe-rior method:
The functional pattern has a great deal of flexibility. It requires less effort than the pseudoclassical pattern, and gives us better encapsulation and information hiding and access to super methods.
If all of the state of an object is private, then the object is tamper-proof. Properties of the object can be replaced or deleted, but the integrity of the object is not compromised.
We can compose objects out of sets of parts.
For example, we can make a function that can add simple X feature to any object.
Useful If contstructors take lots of parameters
Instead of