原生js实现继承

  在多数语言中继承都很重要。JavaScript是一个基于原型的语言,这意味着对象可以直接从其他对象继承。以下列出几种常见的js继承方式。

  1. 原型链继承
    function Father(){ 
     this.status = true; 
    } 
    Father.prototype.getSuper = function(){ 
     return this.status; 
    }; 
    function Son(){ 
     this.substatus = false; 
    } 
    //继承了 SuperType 
    Son.prototype = new Father(); 
    Son.prototype.getSub = function (){ 
     return this.substatus; 
    }; 
    var instance = new Son(); 
    alert(instance.getSuper()); //true

     

  2. 借用构造函数继承
    function Father(){ 
     this.colors = [1,2,3]; 
    } 
    function Son(){ 
     //继承了 Father
     Father.call(this); 
    } 
    var instance1 = new Son(); 
    instance1.colors.push(4); 
    alert(instance1.colors); //"1,2,3,4" 
    var instance2 = new Son(); 
    alert(instance2.colors); //"1,2,3"

     

  3. 组合继承
    function Father(name){ 
      this.name = name; 
    } 
    Father.prototype.sayName = function(){ 
      alert(this.name);
    }; 
    function Son(name, age){ 
      //继承属性
      SuperType.call(this, name); 
      this.age = age; 
    } 
    
    Son.prototype = new Father(); //将子类的原型指向父类
    Son.prototype.constructor = Son; //将子类的constructor指向自己
    Son.prototype.sayAge = function(){ 
      alert(this.age); 
    }; 
    var instance = new Son("lh", 19); 
    instance1.sayName(); //"lh"; 
    instance1.sayAge(); //19 

     

  4. 原型式继承
    function object(o){ 
      function F(){} 
      F.prototype = o; 
      return new F(); 
    }
    var person = { 
      name: "lh", 
      loves: ["a", "b", "c"] 
    }; 
    var anotherPerson = object(person); 
    anotherPerson.name = "Greg"; 
    anotherPerson.friends.push("d");  
    alert(person.loves); //"a,b,c,d"

     这里的object方法和Object.create()方法类似。

  5. 寄生式继承
    function object(o){ 
      function F(){} 
      F.prototype = o; 
      return new F(); 
    }
    function createAnother(obj){ 
      var clone = object(obj); //通过调用函数创建一个新对象
      clone.say = function(){ //给这个对象添加方法
      alert("hi"); 
      }; 
      return clone; //返回这个对象
    }
    var person = { 
      name: "lh", 
      loves: ["a", "b", "c"] 
    }; 
    var anotherPerson = createAnother(person); 
    anotherPerson.say(); //"hi"

     

  6. 寄生组合式继承
    function inheritPrototype(son, father){ 
      var prototype = Object.create(father.prototype); 
       son.prototype = prototype; 
      prototype.constructor = son; } function Father(name){ this.name = name; } Father.prototype.sayName = function(){ alert(this.name); }; function Son(name, age){ Father.call(this, name); this.age = age; } inheritPrototype(Son, Father);

     

  7. 使用es6实现
    class Father{
      constructor(x, y) {
        this.x = x;
        this.y = y;
      }
    }
    class son extends Father{
      constructor(x, y, age) {
        super(x, y); // 调用父类的constructor(x, y)
        this.age= age;
      }
      toString() {
        return this.color + ' ' + super.toString(); // 调用父类的toString()
      }
    }
    子类继承父类中constructor中必须先调用super()方法才能访问this,否则会报错。

转载于:https://www.cnblogs.com/wjgoblin/p/10949939.html

你可能感兴趣的:(原生js实现继承)