JavaScript继承

来自红宝书的几种继承方式

1. 原型链继承

    function SuperClass(){
        this.text = ['hello', 'world'];
    }

    SuperClass.prototype.sayHi = function(){
        console.log(this.text);
    }

    function SubClass(){

    }

    SubClass.prototype = new SuperClass();

    var test1 = new SubClass();
    test1.sayHi();// ["hello", "world"]

    var test2 = new SubClass();
    test2.text.push('shit');

    test1.sayHi(); //["hello", "world", "shit"]  所有实例引用的都是同一个值

缺点:

  1. 原型的属性被所有实例共享
  2. 创建子类时不能向父类传参

2. 借用构造函数继承

    function SuperClass(data){
        this.text = ['hello', 'world'];
        this.data = data;
        this.sayHi = function(){
            console.log(this.text);
        }
    }

    SuperClass.prototype.sayBye = function(){
        console.log(bye);
    }

    function SubClass(data, name){
        SuperClass.call(this, data);
        this.name = name;
    }

    var test1 = new SubClass();
    test1.sayHi(); //["hello", "world"]

    var test2 = new SubClass();
    test2.text.push('shit');

    test1.sayHi(); //["hello", "world"]
    
    test1.sayBye();//Uncaught TypeError: test1.sayBye is not a function

优点:

  1. 避免所有实例共享原型的属性
  2. 可以向父类传参
    缺点:
  3. 只能继承父类的实例属性、方法,不能继承原型上的属性、方法
  4. 方法要在父类构造函数中定义,每次创建子类实例都会创建一个方法的副本。子类实例间的方法相同却不能复用。

3. 组合继承

将原型链继承和借用构造函数继承组合起来使用

    function SuperClass(name) {
        this.text = ['hello', 'world'];
        this.name= name;
        this.sayHi = function () {
            console.log(this.name);
        }
    }

    SuperClass.prototype.sayBye = function () {
        console.log('bye');
    }

    function SubClass(name) {
        SuperClass.call(this, name);
    }

    SubClass.prototype = new SuperClass();
    SubClass.prototype.constructor = SubClass;


    var test1 = new SubClass('one');
    var test2 = new SubClass('two');

    test1.sayHi(); //one
    test2.sayHi(); //two
    test2.sayBye(); //bye

缺点:
调用了两次父类构造函数,实例和原型有一样的属性的方法。
一个来自于SuperClass.call(this, data);
一个来自于SubClass.prototype = new SuperClass()


image.png

4. 原型式继承

利用一个空函数作为中介,进行原型的连接。

    //这个操作其实就是ES5里面的Object.create
    function inherits(obj) {
        function F() { }
        F.prototype = obj;
        return new F();
    }


    function SuperClass(name) {
        this.text = ['hello', 'world'];
        this.name = name;
        this.sayHi = function () {
            console.log(this.name);
        }
    }

    SuperClass.prototype.sayBye = function () {
        console.log('bye');
    }

    var parent = new SuperClass('test');
    var child1 = inherits(parent);
    var child2 = inherits(parent);

    child1.sayHi(); //test
    child1.sayBye(); //bye

    child1.text.push('shit');
    console.log(child2.text); //["hello", "world", "shit"] 实例共享父类属性

缺点:与原型链继承相同

5. 寄生式继承

和原型式继承一个意思

function createObj (o) {
    var clone = Object.create(o);
    clone.sayName = function () {
        console.log('hi');
    }
    return clone;
}

缺点:与原型式继承相同

6. 寄生组合继承

利用空函数来连接原型,代替组合继承中的SubClass.prototype = new SuperClass(),减少了一次父类构造调用

    function SuperClass(name) {
        this.text = ['hello', 'world'];
        this.name = name;
        this.sayHi = function () {
            console.log(this.name);
        }
    }

    SuperClass.prototype.sayBye = function () {
        console.log('bye');
    }

    function SubClass(name) {
        SuperClass.call(this, name);
    }

    var F = function(){};
    F.prototype = SuperClass.prototype;
    SubClass.prototype = new F();
    //以上三行相当于
    //SubClass.prototype =  Object.create(SuperClass.prototype)

    var test1 = new SubClass('one');
    var test2 = new SubClass('two');

    test1.sayHi(); //one
    test2.sayHi(); //two
    test2.sayBye(); //bye

总结:
最好的继承方式是寄生组合继承

    function SubClass() {
        SuperClass.call(this);
    }
    SubClass.prototype = Object.create(SuperClass.prototype);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 也许学过JavaScript继承方式的朋友都知道,自定义引用类型的最佳继承方式非寄生组合继承方式莫属;然而,...
    阿尔卑斯的隆冬阅读 3,878评论 0 1
  • 本文是本人学习,张容铭著的《Javascript 设计模式》的笔记与总结。仅供学习交流。 每个类都有3个部分: 1...
    Joyanceh阅读 4,117评论 1 2
  • 继承的概念:子类可以使用父类共享的属性和方法,避免重复代码提高代码复用性。 原型链:子类可以共享父类的实例对象和实...
    浅秋_6672阅读 3,034评论 0 0
  • 例子 我们生成两个构造函数,后面的例子都是让‘’猫‘’继承‘’动物‘’的所有属性和方法。 动物(为了更好的理解各种...
    流光号船长阅读 2,635评论 0 1
  • 结完了这个月的账,我和小胖子走在下班回去的路上, 看着同事们遗下的笑容结伴而去, 硬性道别的方式有很多种, 我想还...
    卢荒年阅读 1,631评论 2 2