Implementar la herencia de JavaScript

1. ES6 se extiende

Utilice la palabra clave extiende para lograr la herencia entre clases. Esto es mucho más conveniente que usar la herencia en ES5.

//定义类父类
class Animal {
    
       
  constructor(color) {
    
       
    this.color = color;   
  }   
  greet(sound) {
    
       
    console.log(sound);   
  }  
}   
class Dog extends Animal {
    
       
  constructor(color) {
    
       
    super(color);   
    this.color = color;   
  }  
}   

let dog = new Dog('黑色');  
dog.greet('汪汪');  // "汪汪"
console.log(dog.color); // "黑色"


2. Utilice prototipo y llame

   const extend = (Target, Origin) => {
    
    
           // Origin.call(Target)
            function Fun() {
    
     }
            Fun.prototype = Origin.prototype
            Target.prototype = new Fun()
            //Target.prototype = Object.create(Origin.prototype)
            Target.prototype.constructor = Target
            Target.prototype.uber = Origin.prototype
        }
        Father.prototype.b = 1
        function Father() {
    
    
            this.a=2
        }

        function Son() {
    
    
            // Father.call(this)
        }
        extend(Son, Father)
        var son = new Son();
        var father = new Father();
        console.log(son.b)
        console.log(son.a)

3. Herencia combinada de parásitos

function Animal(color) {
    
    
  this.color = color;
  this.name = 'animal';
  this.type = ['pig', 'cat'];
}
Animal.prototype.greet = function(sound) {
    
    
  console.log(sound);
}
function Dog(color) {
    
    
  Animal.apply(this, arguments);
  this.name = 'dog';
}
/* 注意下面两行 */

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.getName = function() {
    
    
  console.log(this.name);
}
var dog = new Dog('白色');   
var dog2 = new Dog('黑色');     
dog.type.push('dog');   
console.log(dog.color);   // "白色"
console.log(dog.type);   // ["pig", "cat", "dog"]
console.log(dog2.type);  // ["pig", "cat"]
console.log(dog2.color);  // "黑色"
dog.greet('汪汪');  //  "汪汪"


La función de una copia superficial de Object.create () es la siguiente:

function create(obj) {
    
    
  function F() {
    
    };
  F.prototype = obj;
  return new F();
}

Una cosa más a tener en cuenta aquí, debido a que el prototipo de Animal se copia y se asigna a Dog.prototype, la propiedad del constructor en Dog.prototype también se ha reescrito, por lo que tenemos que solucionar este problema:Dog.prototype.constructor = Dog;

Utilice cierres para privatizar variables

const inherit = (() => {
    
    
    let F = function () {
    
    }
    return (Target,Origin) => {
    
    
        F.prototype= Origin.prototype
        Target.prototype = new F()
        Target.prototype.constructor = Target
        Target.prototype.uber = Origin.prototype
    }
})()

Análisis integral de la herencia

Supongo que te gusta

Origin blog.csdn.net/pz1021/article/details/105150115
Recomendado
Clasificación