継承と特性(伸びる)JavaScriptで

JS継承:サブクラスは親クラスのプロパティとメソッドを共有し、jsの継承はプロトタイプに基づいて実装されています。

jsが、次の連続に分かれて継承されました:

まず、 `のは、動物のクラスを定義しましょう

function Animal (name) {
    // 属性
    this.name = name;
    // 实例方法
    this.say= function(){
      console.log("My name is "+this.name);
    }
}
// 原型方法
Animal.prototype.eat = function(food) {
    console.log(this.name + '正在吃:' + food);
};

1、プロトタイプチェーン継承:親のクラスのサブクラスのインスタンスに対するプロトタイプ点

function Cat(){
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';


var cat = new Cat();
console.log(cat.name);
cat.say();
cat.eat('fish');
console.log(cat instanceof Animal); //true
console.log(cat instanceof Cat); //true
継承特性へのプロトタイプチェーン
  1.非常纯粹的继承关系,实例是子类的实例,也是父类的实例
  2.父类新增原型方法/原型属性,子类都能访问到
  3.简单,易于实现
継承の欠点にプロトタイプチェーン
1.要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行,不能放到构造器中
2.无法实现多继承
3.来自原型对象的引用属性是所有实例共享的
4.创建子类实例时,无法向构造函数传参

図2に示すように、構造継承(コール、継承を適用​​します)

function Cat(name){
    Animal.call(this,name);
}

var cat = new Cat("Tom");
console.log(cat.name);
cat.say();
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true
構造継承された特性:

図1に示すように、溶液1、サブクラス共有親クラスインスタンス属性が問題に引用
2、インスタンスをサブクラス、パラメータは、親クラスに渡すことができ
3、多重継承(コール複数の親クラス・オブジェクト)を実装することができます

継承された欠点を構造:

図1に示すように、親クラスのインスタンスの例は、サブクラスのインスタンスのみではない
プロトタイプは、プロパティ/メソッドを継承することができない、2、親クラス・インスタンスの属性およびメソッドを継承することができ
3を親クラスの各インスタンスは、サブクラスを有しているため、機能は、多重化することによって達成することができませんコピー機能、パフォーマンスに影響を与えます

3、承継のコピー

function Cat(name){
    var animal = new Animal(name);
    for(var key in animal){
        Cat.prototype[key] = animal[key];
    }
}
// Test Code
var cat = new Cat("Tom");
console.log(cat.name);
cat.say();
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true
継承された特性をコピーします。
1、支持多继承
継承された欠点をコピーします。
1、效率较低,内存占用高(因为要拷贝父类的属性)
2、无法获取父类不可枚举的方法(不可枚举方法,不能使用for in 访问到)

4、継承(組み合わせおよび構成プロトタイプ継承チェーン)の組み合わせ

function Cat(name){
    Animal.call(this,name);
}

Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;

// Test Code
var cat = new Cat();
console.log(cat.name);
cat.say();
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true
継承された特性の組み合わせ

1、モード2の欠点を補うために、インスタンスのプロパティ/メソッドを継承できるプロトタイププロパティ/メソッドを継承することができ
、サブクラス2の両方のインスタンス、例も親クラスである
3、何の参照が存在しない共有属性
引数渡すことができ
、機能4再利用可能

継承された欠点の組み合わせ:
1、调用了两次父类构造函数,生成了两份实例(子类实例将子类原型上的那份屏蔽了)

図5に示すように、寄生継承の組み合わせ

function Cat(name){
    Animal.call(this);
    this.name = name;
}
(function(){
    // 创建一个没有实例方法的类
    var Super = function(){};
    Super.prototype = Animal.prototype;
    //将实例作为子类的原型
    Cat.prototype = new Super();
})();

// Test Code
var cat = new Cat();
console.log(cat.name);
cat.say();
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true

継承された特性の寄生組み合わせ:完璧な
継承された欠点の寄生組み合わせ:より複雑達成するために

6、ES6継承(糖衣構文)

class Animal{
    constructor(name){
        this.name = name;
    }
    say(){
        alert("My name is "+this.name);
    }
    eat(food){
        alert(this.name+" is eating "+food);
    }
}
class Cat extends Animal{
    constructor(name){
        super(name);
    }
}

var  tom = new Cat("Tom");
tom.say();
tom.eat("apple");
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); //true

ES6の特性を継承した:最も快適な

出版元の記事 ウォンの賞賛0 ビュー16

おすすめ

転載: blog.csdn.net/qq_42259050/article/details/104464793