面向对象继承复习

js基础的面向对象的继承

构造函数继承

    function Parent() {
        this.name = 'parent'
    }
    function Child() {
        Parent.call(this)  //原理就是调用parent并且改变this的指向,指向当前的this
        this.type = 'child'
    }

这种方法的缺陷是只会继承构造函数上的实例属性,并不会继承原型对象上的属性,优点就是如果parent上有引用类型的属性,不会共用

借助原型链继承

    function Parent() {
        this.name = 'parent'
    }
    Parent.prototype.say = function () {
        console.log('say hello');
    }
    function Child() {
        this.type = 'child'
    }
    Child.prototype = new Parent //通过原型链继承把实例上的属性和原型上的属性都继承了

缺点就是如果原型上的属性有引用类型的,改变了其中一个实例上的引用属性,另外一个也会改变,

构造函数加原型链的继承方式

    function Parent() {
        this.name = 'parent'
        this.arr = [1,2,3]
    }
    Parent.prototype.say = function () {
        console.log('say hello');
    }
    function Child() {
        Parent.call(this) //继承parent构造函数里面的方法
        this.type = 'child'
    }
    Child.prototype = new Parent //继承构造函数parent原型上的方法

这个综合了两种方法比较完美,但是parent构造函数执行了两次,只是不必要的,也是对资源的一种浪费,稍微优化下就好了

    function Parent() {
        this.name = 'parent'
        this.arr = [1,2,3]
    }
    Parent.prototype.say = function () {
        console.log('say hello');
    }
    function Child() {
        Parent.call(this) //继承parent构造函数里面的方法
        this.type = 'child'
    }
    Child.prototype = Parent.prototype//继承构造函数parent原型上的方法

这里还有个小缺陷,因为Child的原型和Parent的原型是同一个对象,所以constructor都会指向parent,所以就不能区分child构造出来的实例到底是由Child构造的还是Parent构造的
于是还可以优化一下

    function Parent() {
        this.name = 'parent'
        this.arr = [1,2,3]
    }
    Parent.prototype.say = function () {
        console.log('say hello');
    }
    function Child() {
        Parent.call(this) //继承parent构造函数里面的方法
        this.type = 'child'
    }
    Child.prototype = Object.create(Parent.prototype)//为什么要有这一步呢,如果直接执行下面那一步,那么因为Child和Parent的原型是一样的,所以如果直接改了Child原型上的prototype的构造器属性会导致Parent上的构造器属性也一样指向了Child构造函数,这样就失去意义了,所以要根据Object.create来当一个中间对象,隔绝这层联系
    Child.prototype.constructor = Child;//修正一下构造器属性

ES6方式实现继承

    class Parent{
        constructor(name){
            this.name = name
        }
        getName(){
            console.log(this.name);
        }
    }

    class Child extends Parent{ 
        //es6中主要通过extends去继承原型链上面的,super去继承实例上的
        constructor(name,age){
            super(name);
            this.age = age
        }
        getAge(){
            console.log(this.age);
        }
    }

    const child = new Child('jj',18);
    child.getName(); //jj
    child.getAge(); //18

这里的constructor里面的super大概等于之前的 Parent.call(this)

这里就是面向对象继承的内容了

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

推荐阅读更多精彩内容

  •   面向对象(Object-Oriented,OO)的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意...
    霜天晓阅读 2,139评论 0 6
  • 博客内容:什么是面向对象为什么要面向对象面向对象编程的特性和原则理解对象属性创建对象继承 什么是面向对象 面向对象...
    _Dot912阅读 1,449评论 3 12
  • 我知道你听过 爱笑的女人,运气都不会差 我还要告诉你 喜欢做饭的人,活的都不差 这无关乎性别,无关乎单身还是已婚 ...
    孩子趣阅读 510评论 2 7
  • 前言:昨天接孩子放学,叽叽喳喳的都是秋游的事情:玩了什么项目,具体是怎样的过程,我是怎么想的,说了一路。 我:“这...
    遇见应宁阅读 116评论 1 1
  • 佛说 人的执念太多 唯有学会选择 方可美满 执念那么多 偏偏那么多年就喜欢一个回忆里的人 偏偏那么多年就希望所有人...
    hejq阅读 337评论 0 0