1. 程式人生 > >JavaScript中子類調用父類方法的實現

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

lee code java ood left 生成 解決 宋體 PE

一、前言

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

二、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、原型鏈繼承

核心: 將父類的實例作為子類的原型

  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.非常純粹的繼承關系,實例是子類的實例,也是父類的實例;

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

3.簡單,易於實現;

缺點:

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

2.無法實現多繼承;

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

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

2、構造繼承

核心:使用父類的構造函數來增強子類實例,等於是復制父類的實例屬性給子類(沒用到原型)

  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. 無法實現函數復用,每個子類都有父類實例函數的副本,影響性能;

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.無法實現多繼承;

4、拷貝繼承

  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 訪問到);

5、組合繼承

核心:通過調用父類構造,繼承父類的屬性並保留傳參的優點,然後通過將父類實例作為子類原型,實現函數復用

  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. 調用了兩次父類構造函數,生成了兩份實例(子類實例將子類原型上的那份屏蔽了);

6、寄生組合繼承

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

  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. 實現較為復雜;

三、JavaScript中子類調用父類方法的解決方案

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

四、總結

附錄一、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. };

附錄二、參考文章

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

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