继承模式

模式1:默认模式
代码复用大家常用的默认模式,往往是有问题的,该模式使用Parent()的构造函数创建一个对象,并且将该对象赋值给Child()的原型。我们看一下代码:

  1. function inherit(C, P) {
  2.     C.prototype = new P();
  3. }
  4. function Parent(name) {
  5.     this.name = name || 'Adam';
  6. }
  7. Parent.prototype.say = function () {
  8.     return this.name;
  9. };
  10. function Child(name) {
  11. }
  12. inherit(Child, Parent);
  13. var kid = new Child();
  14. console.log(kid.say()); // "Adam"
  15. var kiddo = new Child();
  16. kiddo.name = "Patrick";
  17. console.log(kiddo.say()); // "Patrick"
  18. var s = new Child('Seth');
  19. console.log(s.say()); // "Adam"
复制代码


缺点 1.同时继承了两个对象的属性,即添加到this的属性和原型的属性. 2.Child不能传进参数。

模式2:借用构造函数
该模式是Child借用Parent的构造函数进行apply,然后将child的this和参数传递给apply方法:

  1. function Child(a,b,c,d){
  2.     Parent.apply(this,arguments);
  3. }
  4. function Parent() {
  5.     this.name = [1,2,3];
  6. }
  7. var parent = new Parent();
  8. Parent.prototype.say = function () {
  9.     return this.name;
  10. };
  11. //pattern 2
  12. function Child_2() {
  13.     Parent.apply(this, arguments);
  14. }
  15. //pattern1
  16. function Child_1(){
  17. }
  18. Child_1.prototype = parent;
  19. var kid_pattern1 = new Child_1();
  20. var kid_pattern2 = new Child_2();
  21. console.log(parent.hasOwnProperty("name"));//true
  22. console.log(kid_pattern1.hasOwnProperty("name"));//false
  23. console.log(kid_pattern2.hasOwnProperty("name"));//true
  24. console.log(typeof kid_pattern1.say); // "function"
  25. console.log(typeof kid_pattern2.say); // "undefined"
  26. kid_pattern1.name.push("changeBYPattern1");
  27. kid_pattern2.name.push("changeBYPattern2");
  28. console.log(parent.name);//[1, 2, 3, "changeBYPattern1"]
复制代码


缺点 1.只继承了this对象上的属性,并没有继承到原型的属性(say()),
区别 1.模式2可以获得父对象中name属性的副本,而模式1获得的是父对象中name属性的引用

模式3:借用构造函数并设置原型
上述两个模式都有自己的缺点,那如何把两者的缺点去除呢,我们来尝试一下:

  1. function Parent(name) {
  2.     this.name = name || 'Adam';
  3. }
  4. Parent.prototype.say = function () {
  5.     return this.name;
  6. };
  7. function Child(name) {
  8.     Parent.apply(this, arguments);
  9. }
  10. Child.prototype = new Parent();
  11. var kid = new Child("Patrick");
  12. console.log(kid.name); // "Patrick"
  13. console.log(typeof kid.say); // function
  14. console.log(kid.say()); // Patrick
  15. console.dir(kid);
  16. delete kid.name;
  17. console.log(kid.say()); // "Adam"
复制代码


缺点 1.Parent构造函数执行了两次,所以说,虽然程序可用,但是效率很低。

模式4:共享原型
共享原型是指Child和Parent使用同样的原型,代码如下:

  1. function inherit(C, P) {
  2.     C.prototype = P.prototype;
  3. }
  4. function Parent(name) {
  5.     this.name = name || 'Adam';
  6. }
  7. Parent.prototype.say = function () {
  8.     return this.name;
  9. };
  10. function Child(name) {
  11. }
  12. inherit(Child, Parent);
  13. var kid = new Child('Patrick');
  14. console.log(kid.name); // undefined
  15. console.log(typeof kid.say); // function
  16. kid.name = 'Patrick';
  17. console.log(kid.say()); // Patrick
  18. console.dir(kid);
复制代码


缺点 1.Child的参数没有正确接收到. 2.如果子对象修改了原型会影响到所有的祖先对象.

模式5:临时构造函数
首先借用构造函数,然后将Child的原型设置为该借用构造函数的实例,最后恢复Child原型的构造函数。代码如下:

  1. var inherit = (function () {
  2.     var F = function () {
  3.     };
  4.     return function (C, P) {
  5.         F.prototype = P.prototype;
  6.         C.prototype = new F();
  7.         C.uber = P.prototype;//存储超类(super是保留的关键字)
  8.         C.prototype.constructor = C;//重置构造函数,如果不重置,子对象的构造函数会指向Parent
  9.     }
  10. } ());
  11. function Parent(name) {
  12.     this.name = name || 'Adam';
  13. }
  14. Parent.prototype.say = function () {
  15.     return this.name;
  16. };
  17. function Child(name) {
  18. }
  19. inherit(Child, Parent);
  20. var kid = new Child("Patrick");
  21. console.log(kid.name); // undefined
  22. console.log(typeof kid.say); // function
  23. kid.name = 'Patrick';
  24. console.log(kid.say()); // Patrick
  25. console.log(kid.constructor.name); // Child
  26. console.log(kid.constructor === Parent); // false
复制代码


为了避免每次需要继承时都要创建一个代理构造函数可以使用闭包来储存,进行优化

  1. var inherit = (function(){
  2.     var F = function () {};
  3.     return function(C,P){
  4.         F.prototype = P.prototype;
  5.         C.prototype = new F();
  6.         C.uber = P.prototype;
  7.         C.prototype.constructor = C;
  8.     }
  9. })()
复制代码


缺点 1.Child不能正常接收参数.
区别 1.和模式1的区别在于,子类对象只继承了原型对象上的成员,而没有继承到this对象的成员,这也正是我们所需要的.因为原型上的才是可复用的

模式6:klass

  1. var klass = function (Parent, props) {
  2.     var Child, F, i;
  3.     Child = function () {
  4.         if (Child.uber && Child.uber.hasOwnProperty("__construct")) {
  5.             Child.uber.__construct.apply(this, arguments);
  6.         }
  7.         if (Child.prototype.hasOwnProperty("__construct")) {
  8.             Child.prototype.__construct.apply(this, arguments);
  9.         }
  10.     };
  11.     Parent = Parent || Object;
  12.     F = function () {
  13.     };
  14.     F.prototype = Parent.prototype;
  15.     Child.prototype = new F();
  16.     Child.uber = Parent.prototype;
  17.     Child.prototype.constructor = Child;
  18.     for (i in props) {
  19.         if (props.hasOwnProperty(i)) {
  20.             Child.prototype[i] = props[i];
  21.         }
  22.     }
  23.     // return the "class"
  24.     return Child;
  25. };
  26. var Man = klass(null, {
  27.     __construct: function (what) {
  28.         console.log("Man's constructor");
  29.         this.name = what;
  30.     },
  31.     getName: function () {
  32.         return this.name;
  33.     }
  34. });
  35. var first = new Man('Adam'); // logs "Man's constructor"
  36. first.getName(); // "Adam"
  37. var SuperMan = klass(Man, {
  38.     __construct: function (what) {
  39.         console.log("SuperMan's constructor");
  40.     },
  41.     getName: function () {
  42.         var name = SuperMan.uber.getName.call(this);
  43.         return "I am " + name;
  44.     }
  45. });
  46. var clark = new SuperMan('Clark Kent');
  47. clark.getName(); // "I am Clark Kent"
  48. console.log(clark instanceof Man); // true
  49. console.log(clark instanceof SuperMan); // true
复制代码


缺点 1.看起来十分混乱.

原文地址:https://www.cnblogs.com/rainbow661314/p/3384691.html