官术网_书友最值得收藏!

  • JavaScript:Moving to ES2015
  • Ved Antani Simon Timms Narayan Prusty
  • 519字
  • 2021-07-09 19:07:47

Inheritance

One of the niceties of objects is that they can be built upon to create increasingly 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 like so:

let Castle = function(){};
Castle.prototype.build = function(){console.log("Castle built");}

let Winterfell = function(){};
Winterfell.prototype.build = Castle.prototype.build;
Winterfell.prototype.addGodsWood = function(){}
let 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 na?ve fashion like this:

function clone(source, destination) {
  for(var attr in source.prototype){ destination.prototype[attr] = source.prototype[attr];}
}

If you are into object diagrams this shows how Winterfell extends Castle in this diagram:

This can be used quite simply as follows:

let Castle = function(){};
Castle.prototype.build = function(){console.log("Castle built");}

let Winterfell = function(){};
clone(Castle, Winterfell);
let winterfell = new Winterfell();
winterfell.build();

We say that this is na?ve 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 used extensively, internally in jQuery 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. Most modern languages only support single inheritance: a class can have only one direct parent. There are some languages with 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.

主站蜘蛛池模板: 湖口县| 威远县| 珲春市| 孟津县| 金门县| 海兴县| 湄潭县| 保定市| 平凉市| 丰都县| 邳州市| 六枝特区| 井研县| 黄骅市| 广宗县| 巴青县| 泉州市| 苍梧县| 额济纳旗| 青州市| 沙雅县| 高碑店市| 天峻县| 三原县| 景泰县| 穆棱市| 桂东县| 班戈县| 叙永县| 二手房| 陈巴尔虎旗| 临江市| 东台市| 青阳县| 武川县| 古交市| 织金县| 纳雍县| 扶沟县| 阿巴嘎旗| 凤庆县|