- Mastering JavaScript Design Patterns
- Simon Timms
- 523字
- 2021-08-05 17:14:57
Inheritance
One of the niceties of objects is that they can be built upon to create increasingly more complex objects. This is a common pattern, which is used for any number of things. There is no inheritance in JavaScript because of its prototypical nature. However, you can combine functions from one prototype into another.
Let's say that we have a base class called Castle
and we want to customize it into a more specific class called Winterfell
. We can do so by first copying all of the properties from the Castle
prototype onto the Winterfell
prototype. This can be done as shown in the following code:
var Castle = function(){}; Castle.prototype.build = function(){console.log("Castle built");} var Winterfell = function(){}; Winterfell.prototype.build = Castle.prototype.build; Winterfell.prototype.addGodsWood = function(){} var winterfell = new Winterfell(); winterfell.build(); //prints "Castle built" to the console
Of course, this is a very painful way to build objects. You're forced to know exactly which functions the base class has to copy them. It can be abstracted in a rather naive fashion as follows:
function clone(source, destination) { for(var attr in source.prototype){ destination.prototype[attr] = source.prototype[attr];} }
The following is the class diagram of the Castle
class:

The following class code can be used quite simply:
var Castle = function(){}; Castle.prototype.build = function(){console.log("Castle built");}; var Winterfell = function(){}; clone(Castle, Winterfell); var winterfell = new Winterfell(); winterfell.build();
We say that this is naive because it fails to take into account a number of potential failure conditions. A fully fledged implementation is quite extensive. The jQuery library provides a function called extend
which implements prototype inheritance in a robust fashion. It is about 50 lines long and deals with deep copies and null values. The function is internally used in jQuery extensively, but it can be a very useful function in your own code. We mentioned that prototype inheritance is more powerful than the traditional methods of inheritance. This is because it is possible to mix and match bits from many base classes to create a new class. In most modern languages, there is support for only single inheritance: a class can have only one direct parent. There are some languages where there is multiple inheritance, however, it is a practice that adds a great deal of complexity when attempting to decide which version of a method to call at runtime. Prototype inheritance avoids many of these issues by forcing selection of a method at assembly time.
Composing objects in this fashion permits taking properties from two or more different bases. There are many times when this can be useful. For example, a class representing a wolf might take some of its properties from a class describing a dog and some from another class describing a quadruped.
By using classes built in this way, we can meet pretty much all of the requirements for constructing a system of classes including inheritance. However, inheritance is a very strong form of coupling. In almost all cases, it is better to avoid inheritance in favor of a looser form of coupling. This will allow for classes to be replaced or altered with a minimum impact on the rest of the system.
- C語言程序設(shè)計案例教程(第2版)
- Mastering ServiceStack
- R語言編程指南
- 匯編語言程序設(shè)計(第2版)
- Symfony2 Essentials
- 青少年信息學(xué)競賽
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- UI設(shè)計基礎(chǔ)培訓(xùn)教程(全彩版)
- IPython Interactive Computing and Visualization Cookbook
- 現(xiàn)代C++語言核心特性解析
- C語言編程魔法書:基于C11標(biāo)準(zhǔn)
- Java基礎(chǔ)案例教程(第2版)
- C/C++程序設(shè)計教程:面向?qū)ο蠓謨?/a>
- Mastering Web Application Development with Express
- ASP.NET MVC 4 Mobile App Development