zoukankan      html  css  js  c++  java
  • js继承方式

    原型继承

    将子构造函数的prototype指向父构造函数的实例达到继承的目的

    function Person(name){
        this.name = name;
        this.country='china';
    }
    Person.prototype.play = function(){
        
    }
    
    function Child(age){
        this.age = age;
    }
    
    Child.prototype = new Person();

    缺点:

    • 创建子类实例时,但是无法给父构造函数传参
    • 来自原型对象的引用属性是所有实例共享的

    构造函数继承

    在子类构造函数中调用父类构造函数

    function Person(name){
        this.name = name;
        this.country='china';
    }
    Person.prototype.play = function(){
        
    }
    
    function Child(name,age){
        this.age = age;
        Person.call(this,name)
    }
    var obj = new Child('小明',16);
    

     缺点:

    • 当于每个实例都拷贝了一份父类的方法,占用内存大
    • 不能继承原型属性/方法,只能继承父类的实例属性和方法

    组合式继承

    使用原型链实现对原型属性和方法的继承,通过借用构造函数实现对实例属性的继承

    function Person(name){
        this.name = name;
        this.country='china';
    }
    Person.prototype.play = function(){
        
    }
    function Child(name,age){
        Person.call(this,name);
        this.age = age;
    }
    Child.prototype = new Person();
    

     缺点:

    • 会调用两次父类构造函数

    原型式继承

    寄生式继承

    寄生组合式继承

    ES6继承

    原拷贝继承

  • 相关阅读:
    禅道
    centos7 安装redis 出现cc: command not found错误解决
    Linux 安装 redis
    vuex store modules
    vuex store 改造
    vuex store
    Vue axios
    Vue keep-alive
    vue 路由守卫
    vue-router 参数传递
  • 原文地址:https://www.cnblogs.com/liuxiaoru/p/13628447.html
Copyright © 2011-2022 走看看