JavaScript中子類調用父類方法的實現

1、前言

最近在項目中,前端框架使用JavaScript面向對象編程,遇到了諸多問題,其中最典型的問題就是子類調用父類(super class)同名方法,也就是如C#中子類中調用父類函數base.**。如下摘錄了園友幻天芒JavaScript實現繼承的幾種方式 的具體介紹以做備忘。 html

2、JavaScript實現繼承的幾種方式

既然要實現繼承,那麼咱們首先得有一個基類,代碼以下: 前端

  1. // 定義一個動物類
  2.     function Animal(name) {
  3.         // 屬性
  4.         this.name = name || 'Animal';
  5.         // 實例方法
  6.         this.sleep = function () {
  7.             console.log(this.name + '正在睡覺!');
  8.         }
  9.     }
  10.     // 原型方法
  11.     Animal.prototype.eat = function (food) {
  12.         console.log(this.name + '正在吃:' + food);
  13.     };

一、原型鏈繼承 編程

核心: 將父類的實例做爲子類的原型 數組

  1. //定義動物貓
  2. function Cat() {
  3. }
  4. Cat.prototype = new Animal();
  5. Cat.prototype.name = 'cat';
  6.  
  7. // Test Code
  8. var cat = new Cat();
  9. console.log(cat.name); //cat
  10. cat.eat('fish'); //cat正在吃:fish
  11. cat.sleep(); //cat正在睡覺
  12. console.log(cat instanceof Animal); //true
  13. console.log(cat instanceof Cat); //true

特色: 前端框架

1.很是純粹的繼承關係,實例是子類的實例,也是父類的實例; app

2.父類新增原型方法/原型屬性,子類都能訪問到; 框架

3.簡單,易於實現; 函數

缺點: 性能

1.要想爲子類新增屬性和方法,必需要在new Animal()這樣的語句以後執行,不能放到構造器中 測試

2.沒法實現多繼承;

3.建立子類時,沒法向父類構造函數傳參;

4.來自原型對象的屬性是全部實例所共享;

二、構造繼承

核心:使用父類的構造函數來加強子類實例,等因而複製父類的實例屬性給子類(沒用到原型)

  1. function Cat(name) {
  2.     Animal.call(this);
  3.     this.name = name || 'Tom';
  4. }
  5.  
  6. // Test Code
  7. var cat = new Cat();
  8. console.log(cat.name);
  9. console.log(cat.sleep());
  10. console.log(cat instanceof Animal); // false
  11. console.log(cat instanceof Cat); // true

特色:

1. 解決了1中,子類實例共享父類引用屬性的問題;

2. 建立子類實例時,能夠向父類傳遞參數;

3. 能夠實現多繼承(call多個父類對象);

缺點:

1. 實例並非父類的實例,只是子類的實例;

2. 只能繼承父類的實例屬性和方法,不能繼承原型屬性/方法;

3. 沒法實現函數複用,每一個子類都有父類實例函數的副本,影響性能;

三、實例繼承

核心:爲父類實例添加新特性,做爲子類實例返回

  1. function Cat(name) {
  2.     var instance = new Animal();
  3.     instance.name = name || 'Tom';
  4.     return instance;
  5. }
  6.  
  7. // Test Code
  8. var cat = new Cat();
  9. console.log(cat.name);
  10. console.log(cat.sleep());
  11. console.log(cat instanceof Animal); // true
  12. console.log(cat instanceof Cat); // false

特色:

1. 不限制調用方式,不論是new 子類()仍是子類(),返回的對象具備相同的效果;

缺點:

2.沒法實現多繼承;

四、拷貝繼承

  1. function Cat(name) {
  2.     var animal = new Animal();
  3.     for (var p in animal) {
  4.         Cat.prototype[p] = animal[p];
  5.     }
  6.     Cat.prototype.name = name || 'Tom';
  7. }
  8.  
  9. // Test Code
  10. var cat = new Cat();
  11. console.log(cat.name);
  12. console.log(cat.sleep());
  13. console.log(cat instanceof Animal); // false
  14. console.log(cat instanceof Cat); // true

特色:

1. 支持多繼承;

缺點:

1. 效率較低,內存佔用高(由於要拷貝父類的屬性);

2. 沒法獲取父類不可枚舉的方法(不可枚舉方法,不能使用for in 訪問到);

五、組合繼承

核心:經過調用父類構造,繼承父類的屬性並保留傳參的優勢,而後經過將父類實例做爲子類原型,實現函數複用

  1. function Cat(name) {
  2.     Animal.call(this);
  3.     this.name = name || 'Tom';
  4. }
  5. Cat.prototype = new Animal();
  6. Cat.prototype.constructor = Cat;
  7.  
  8. // Test Code
  9. var cat = new Cat();
  10. console.log(cat.name);
  11. console.log(cat.sleep());
  12. console.log(cat instanceof Animal); // true
  13. console.log(cat instanceof Cat); // true

特色:

1.彌補了方式2的缺陷,能夠繼承實例屬性/方法,也能夠繼承原型屬性/方法;

2.既是子類的實例,也是父類的實例;

3.不存在引用屬性共享問題;

4.可傳參;

5.函數可複用;

缺點:

1. 調用了兩次父類構造函數,生成了兩份實例(子類實例將子類原型上的那份屏蔽了);

六、寄生組合繼承

核心:經過寄生方式,砍掉父類的實例屬性,這樣,在調用兩次父類的構造的時候,就不會初始化兩次實例方法/屬性,避免的組合繼承的缺點

  1. function Cat(name){
  2.     Animal.call(this);
  3.     this.name = name || 'Tom';
  4. }
  5. (function(){
  6.     // 建立一個沒有實例方法的類
  7.     var Super = function(){};
  8.     Super.prototype = Animal.prototype;
  9.     //將實例做爲子類的原型
  10.     Cat.prototype = new Super();
  11.     Cat.prototype.constructor = Cat; // 須要修復下構造函數
  12. })();
  13.  
  14. // Test Code
  15. var cat = new Cat();
  16. console.log(cat.name);
  17. console.log(cat.sleep());
  18. console.log(cat instanceof Animal); // true
  19. console.log(cat instanceof Cat); //true

特色:

1. 堪稱完美;

缺點:

1.實現較爲複雜;

3、實現子類調用父類方法的解決方案

上述的幾種繼承方式,如要實現子類Cat,在eat方法中調用父類Animal的eat方法,該如何實現呢? 以寄生組合繼承爲例:

  1. // 定義一個動物類
  2. function Animal(name) {
  3.     // 屬性
  4.     this.name = name || 'Animal';
  5.     // 實例方法
  6.     this.sleep = function () {
  7.         console.log(this.name + '正在睡覺!');
  8.     }
  9. }
  10. // 原型方法
  11. Animal.prototype.eat = function (food) {
  12.     console.log("Father." + this.name + '正在吃:' + food);
  13. };
  14. function Cat(name) {
  15.     Animal.call(this);
  16.     this.name = name || 'Tom';
  17. }
  18. (function () {
  19.     // 建立一個沒有實例方法的類
  20.     var Super = function () { };
  21.     Super.prototype = Animal.prototype;
  22.     //將實例做爲子類的原型
  23.     Cat.prototype = new Super();
  24.     Cat.prototype.constructor = Cat; // 須要修復下構造函數
  25.     Cat.prototype.eat = function (food) {
  26.         console.log("SubClass." + this.name + '正在吃:' + food);
  27.         Super.prototype.eat.apply(this, Array.prototype.slice.apply(arguments));
  28.     };
  29. })();
  30.  
  31. // Test Code
  32. var cat = new Cat();
  33. console.log(cat.name);
  34. cat.eat('fish'); //cat正在吃:fish
  35. console.log(cat instanceof Animal); // true
  36. console.log(cat instanceof Cat); //true

測試結果:

其中的關鍵代碼在於:

咱們看到,子類要想調用父類的方法,必須使用父類原型方法的apply(Super.prototype.eat.apply), 也就是必須得在子類中使用父類對象,這種以硬編碼方式的調用,雖然能解決問題,但確不是特別優雅。

4、優化後的子類調用父類方法的實現方式

以上的繼承方式,都不能實現子類調用父類方法, 在重寫子類原型函數後,會將繼承父類函數給覆蓋。先來看實例:

父類:

  1. var Animal = Class.extend({
  2.     construct: function (name) {
  3.         arguments.callee.father.construct.call(this);
  4.         this.name = name || "Animal";
  5.     },
  6.     eat: function (food) {
  7.         console.log("Father." + this.name + '正在吃:' + food);
  8.     },
  9.     sleep: function () {
  10.         console.log("Father." + this.name + '正在睡覺!');
  11.     }
  12. });

子類:

  1. var Cat = Animal.extend({
  2.     construct: function () {
  3.         arguments.callee.father.construct.call(this,"Cat");
  4.     },
  5.     eat: function (food) {
  6.         console.log("SubClass." + this.name + '正在吃:' + food);
  7.         arguments.callee.father.eat.call(this, food);
  8.     },
  9.     sleep: function () {
  10.         console.log("SubClass." + this.name + '正在睡覺!');
  11.         arguments.callee.father.sleep.call(this);
  12.     }
  13. });

測試代碼:

  1. // Test Code
  2. var cat = new Cat();
  3. console.log(cat.name);
  4. console.log(cat.eat('fish'));
  5. console.log(cat.sleep());
  6. console.log(cat instanceof Animal); // true
  7. console.log(cat instanceof Cat); //true

輸出結果:

在實例中,咱們看到cat實例對象,在eat函數中,既調用了Cat類的eat函數,也調用了Animal類的eat函數,實現了咱們想要達到的效果。實現該種繼承的關鍵點在於Class基類中的處理,爲子類每一個function對象,都添加了father屬性,指向父類的原型(prototype

優化的設計,它使用了JavaScript中一點不爲人知的特性:callee。
在任何方法執行過程當中,你能夠查看那些經過"arguments"數組傳入的參數,這是衆所周知的,但不多有人知道"arguments"數組包含一個名爲"callee"的屬性,它做爲一個引用指向了當前正在被執行的function,然後經過"father"即可以方便的得到當前被執行function所在類的父類。這是很是重要的,由於它是得到此引用的惟一途徑(經過"this"對象得到的function引用老是指向被子類重載的function,然後者並不是全是正在被執行的function)。

這種實現方式相似於C#中base.**的用法調用,而不須要在子類中出現父類名稱的硬編碼,但也存在缺點,不可以實現多繼承。

5、總結

附錄1、Class基類

  1. //定義最頂級類,用於js繼承基類
  2. function Class() { }
  3. Class.prototype.construct = function () { };
  4. Class.extend = function (def) {
  5.     var subClass = function () {
  6.         if (arguments[0] !== Class) { this.construct.apply(this, arguments); }
  7.     };
  8.  
  9.     var proto = new this(Class);
  10.  
  11.     var superClass = this.prototype;
  12.     for (var n in def) {
  13.         var item = def[n];
  14.         if (item instanceof Function) item.father = superClass;
  15.         proto[n] = item;
  16.     }
  17.     subClass.prototype = proto;
  18.  
  19.     //賦給這個新的子類一樣的靜態extend方法
  20.     subClass.extend = this.extend;
  21.     return subClass;
  22. };

附錄2、寄生組合繼承實現多繼承,並實現子類調用父類方法案例

  1. // 定義一個房間類
  2. function Room() {
  3.     // 屬性
  4.     this.name = name || 'Zoom';
  5. }
  6. // 原型方法
  7. Room.prototype.open = function () {
  8.     console.log("Father." + this.name + '正在開門');
  9. };
  10.  
  11.  
  12. // 定義一個動物類
  13. function Animal(name) {
  14.     // 屬性
  15.     this.name = name || 'Animal';
  16.     // 實例方法
  17.     this.sleep = function () {
  18.         console.log(this.name + '正在睡覺!');
  19.     }
  20. }
  21. // 原型方法
  22. Animal.prototype.eat = function (food) {
  23.     console.log("Father." + this.name + '正在吃:' + food);
  24. };
  25. function Cat(name) {
  26.     Animal.call(this);
  27.     Room.call(this);
  28.     this.name = name || 'Tom';
  29. }
  30. // Cat類
  31. (function () {
  32.     Cat.prototype.constructor = Cat;
  33.  
  34.     Cat.prototype.eat = function (food) {
  35.         console.log("SubClass." + this.name + '正在吃:' + food);
  36.         Animal.prototype.eat.apply(this, Array.prototype.slice.apply(arguments));
  37.     };
  38.  
  39.     Cat.prototype.open = function () {
  40.         console.log("SubClass." + this.name + '正在開門');
  41.         Room.prototype.open.apply(this, Array.prototype.slice.apply(arguments));
  42.     };
  43. })();
  44.  
  45. // Test Code
  46. var cat = new Cat();
  47. console.log(cat.name);
  48. cat.open();
  49. cat.eat('fish'); //cat正在吃:fish
  50. console.log(cat instanceof Animal); // false
  51. console.log(cat instanceof Cat); //true

測試結果:

附錄3、參考文章

    https://blog.csdn.net/rainxie_/article/details/39991173

相關文章
相關標籤/搜索