1. 组合继承:又叫伪经典继承,是指将原型链和借用构造函数技术组合在一块的一种继承方式。

下面来看一个例子:

  1. function SuperType(name) {
  2. this.name = name;
  3. this.colors = ["red", "blue", "green"];
  4. }
  5. SuperType.prototype.sayName = function() {
  6. alert(this.name);
  7. }
  8. function SubType(name, age) {
  9. SuperType.call(this, name);
  10. this.age = age;
  11. }
  12. //继承方法
  13. SubType.prototype = new SuperType();
  14. SubType.prototype.sayAge = function() {
  15. alert(this.age);
  16. }
  17. var instance1 = new SubType("Nicholas", 29);
  18. instance1.colors.push("black");
  19. alert(instance1.colors); //red,blue,green,black
  20. instance1.sayName(); //Nicholas
  21. instance1.sayAge(); //29
  22. var instance2 = new SubType("Greg", 27);
  23. alert(instance2.colors); //red,blue,green
  24. instance2.sayName(); //Greg
  25. instance2.sayAge(); //27

组合继承避免了原型链和借用构造函数的缺陷,融合它们的优点。

2. 原型式继承

可以在不必预先定义构造函数的情况下实现继承,其本质是执行对给定对象的浅复制。而复制得到的副本还可以得到进一步的改造。

  1. function object(o) {
  2. function F(){};
  3. F.prototype = o;
  4. return new F;
  5. }
  6. var person = {
  7. name: "Nicholas",
  8. friends: ["Shelby", "Court", "Van"]
  9. };
  10. var antherPerson = object(person);
  11. antherPerson.name = "Greg";
  12. antherPerson.friends.push("Rob");
  13. var antherPerson = object(person);
  14. antherPerson.name = "Linda";
  15. antherPerson.friends.push("Barbie");
  16. alert(person.friends); //Shelby,Court,Van,Rob,Barbie
 

3. 寄生式继承

与原型式继承非常相似,也是基于某个对象或某些信息创建一个对象,然后增强对象,最后返回对象。为了解决组合继承模式由于多次调用超类型构造函数而导致的低效率问题,可以将这个模式与组合继承一起使用。

  1. function object(o) {
  2. function F(){};
  3. F.prototype = o;
  4. return new F;
  5. }
  6. function createAnother(original) {
  7. var clone = object(original);
  8. clone.sayHi = function() {
  9. alert("Hi");
  10. };
  11. return clone;
  12. }
  13. var person = {
  14. name: "Nicholas",
  15. friends: ["Shelby", "Court", "Van"]
  16. };
  17. var anotherPerson = createAnother(person);
  18. anotherPerson.sayHi();

 

4. 寄生组合式继承

集寄生式继承和组合继承的优点与一身,是实现基本类型继承的最有效方式。

  1. //继承原型
  2. function extend(subType, superType) {
  3. function F(){};
  4. F.prototype = superType.prototype;
  5. var prototype = new F;
  6. prototype.constructor = subType;
  7. subType.prototype = prototype;
  8. }
  9. //超类方法
  10. function SuperType(name) {
  11. this.name = name;
  12. this.colors = ["red", "blue", "green"];
  13. }
  14. SuperType.prototype.sayName = function() {
  15. return this.name;
  16. }
  17. //子类方法
  18. function SubType(name, age) {
  19. SuperType.call(this, name);
  20. this.age = age;
  21. }
  22. //继承超类的原型
  23. extend(SubType, SuperType);
  24. //子类方法
  25. SubType.prototype.sayAge = function() {
  26. return this.age;
  27. }
  28. var instance1 = new SubType("Shelby");
  29. var instance2 = new SubType("Court", 28);
  30. instance1.colors.push('black');
  31. alert(instance1.colors); //red,blue,green,black
  32. alert(instance2.colors); //red,blue,green
  33. alert(instance1 instanceof SubType); //true
  34. alert(instance1 instanceof SuperType); //true

这段例子的高效率体现在它只调用了一次SuperType构造函数,并且因此避免了在SubType.prototype上面创建不必要的多余的属性。与此同时,原型链还能保持不变。因此,还能正常使用instanceof 和 isPrototypeOf()。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。

版权声明:本文为Joe-and-Joan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Joe-and-Joan/p/10286888.html