JS继承

作者: Marshall3572 | 来源:发表于2021-03-23 10:16 被阅读0次

    原型链继承

    function Parent () {
        this.name = 'marshall';
    }
    
    Parent.prototype.getName = function () {
        console.log(this.name);
    }
    
    function Child () {
    
    }
    
    Child.prototype = new Parent();
    
    var child1 = new Child();
    
    console.log(child1.getName()) // marshall
    
    1. 这种继承方式中,引用类型的属性被所有实例共享
      e.g.
    function Parent () {
        this.names = ['kevin', 'daisy'];
    }
    
    function Child () {
    
    }
    
    Child.prototype = new Parent();
    
    var child1 = new Child();
    
    child1.names.push('yayu');
    
    console.log(child1.names); // ["kevin", "daisy", "yayu"]
    
    var child2 = new Child();
    
    console.log(child2.names); // ["kevin", "daisy", "yayu"]
    
    1. 在创建 Child 的实例时,不能向Parent传参

    借用构造函数(经典继承)

    function Parent () {
        this.names = ['kevin', 'daisy'];
    }
    
    function Child () {
        Parent.call(this);
    }
    
    var child1 = new Child();
    
    child1.names.push('yayu');
    
    console.log(child1.names); // ["kevin", "daisy", "yayu"]
    
    var child2 = new Child();
    
    console.log(child2.names); // ["kevin", "daisy"]
    

    优点:

    1. 避免了引用类型的属性被所有实例共享
    2. 可以在 Child 中向 Parent 传参
    function Parent (name) {
        this.name = name;
    }
    
    function Child (name) {
        Parent.call(this, name);
    }
    
    var child1 = new Child('kevin');
    
    console.log(child1.name); // kevin
    
    var child2 = new Child('daisy');
    
    console.log(child2.name); // daisy
    

    缺点:
    方法都在构造函数中定义,每次创建实例都会创建一遍方法。

    组合继承(原型链和经典继承)

    function Parent (name) {
        this.name = name;
        this.colors = ['red', 'blue', 'green'];
    }
    
    Parent.prototype.getName = function () {
        console.log(this.name)
    }
    
    function Child (name, age) {
    
        Parent.call(this, name);
        
        this.age = age;
    
    }
    
    Child.prototype = new Parent();
    Child.prototype.constructor = Child;
    
    var child1 = new Child('kevin', '18');
    
    child1.colors.push('black');
    
    console.log(child1.name); // kevin
    console.log(child1.age); // 18
    console.log(child1.colors); // ["red", "blue", "green", "black"]
    
    var child2 = new Child('daisy', '20');
    
    console.log(child2.name); // daisy
    console.log(child2.age); // 20
    console.log(child2.colors); // ["red", "blue", "green"]
    

    优点:融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。

    class继承

    class Parent{
      constructor(name1){
        this.name1 = name1
      }
      pMethod(){
        console.log(this.name1)
      }
    }
    class Child extends Parent{ //用 A extends B 来创建父子关系
      constructor(name2, name1){
        super(name1) //super 关键字调用父对象上的函数
        this.name2 = name2 //使用 this 之前,必须先调用super(),否则会抛出错误
      }
      cMethod(){
        console.log(this.name2)
      }
    }
    
    let fn = new Child('hee', 'jack')
    fn.pMethod() //jack
    fn.cMethod() //hee
    

    相关文章

      网友评论

          本文标题:JS继承

          本文链接:https://www.haomeiwen.com/subject/pxbxhltx.html