1. 原型链继承 ```javascript function SuperType(){ this.property = true; }

    SupeType.prototype.getSuperValue = function(){ return this.property; }

    function SubType(){ this.subProperty = false; }

    SubType.prototype = new SuperType(); SubType.prototype.getSubValue = function(){ return this.subproperty; }

    弊端: 父类构造函数初始化的属性在原型链上, 对父类属性的更改将会印象到每一个实例。 父类构造函数初始化的属性应该是实例化对象的属性

    如以下对colors的修改将会影响两个实例: function SuperType(){ this.colors = [“red”, “blue”, “green”]; } function SubType(){}

    SubType.prototype = new SuperType();

    var instance1 = new SubType(); instance1.colors.push(“black”); alert(instance1.colors); //“red,blue,green,black”

    var instance2 = new SubType(); alert(instance2.colors); //“red,blue,green,black”

    1. 2. 构造函数继承
    2. ```javascript
    3. function SuperType(){
    4. this.colors = ["red", "green", "blue"];
    5. }
    6. function SubType() {
    7. SuperType.call(this);
    8. }
    9. 解决了原型链继承的弊端(父类构造函数初始化的属性在原型链上, 对父类属性的更改将会印象到每一个实例。)
    10. 弊端:
    11. 只能继承父类的属性和方法(调用父类构造函数),不能继承原型属性和方法
    12. 由于每个实例都会调用父类构造函数,所以每个子类都有父类的实例属性和对象。影响性能。
    1. 组合继承 ```javascript function SuperType(name){ this.name = name; this.colors = [‘red’, ‘blue’, ‘green’]; }

    SuperType.prototype.sayName = function(){ alert(this.name); }

    function SubType(name, age){ SuperType.call(this, name) this.age = age; }

    SubType.prototype = new SuperType() SubType.prototype.constructor = SubType SubType.prototype.sayAge = function(){ alert(this.age) }

    弊端: 由于每个实例都会调用父类构造函数,所以每个子类都有父类的实例属性和对象。影响性能。

    1. 4. 原型式继承
    2. ```javascript
    3. function object(obj) {
    4. function F(){}
    5. F.prototype = obj;
    6. return new F();
    7. }
    8. var person = {
    9. name: "Nicholas",
    10. friends: ["Shelby", "Court", "Van"]
    11. };
    12. var anotherPerson = object(person);
    13. anotherPerson.name = "Greg";
    14. anotherPerson.friends.push("Rob");
    15. var yetAnotherPerson = object(person);
    16. yetAnotherPerson.name = "Linda";
    17. yetAnotherPerson.friends.push("Barbie");
    18. alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"
    19. 弊端:
    20. 多个实例对象原型引用相同, 存在篡改的可能
    21. 无法传递参数
    1. 寄生式继承

      1. function createAnother(original) {
      2. let clone = object(original)
      3. clone.sayHi = function() {
      4. alert("hi")
      5. }
      6. return clone
      7. }
      8. 主要是增强了函数
      9. 弊端同原型式继承
    2. 寄生组合式继承(最成熟的方法) ```javascript function inheritPrototype(subtype, supertype){ let prototype = Object.create(supertype.prototype); prototype.constructor = subtype; subtype.prototype = prototype; }

    function SuperType(name) { this.name = name; this.colors = [‘red’, ‘blue’, ‘green’]; }

    SuperType.prototype.sayName = function() { alert(this.name) }

    function SubType(age) { SuperType.call(this, name); this.age = age; }

    inheritPrototype(SubType, SuperType); SubType.prototype.sayAge = function() { alert(this.age) }

    1. 7. ES6类继承extends
    2. ```javascript
    3. // 继承核心代码
    4. function _inherites(subType, superType) {
    5. subType.prototype = Object.create(superType && superType.prototype, {
    6. constructor: {
    7. value: subType,
    8. enumerable: false,
    9. writable: true,
    10. configurable: true,
    11. }
    12. })
    13. if(superType) {
    14. Object.setPrototypeOf
    15. ? Object.setPrototypeOf(subType, superType)
    16. : subType.__proto__ = superType
    17. }
    18. }