JavaScript原型链、继承

本文详细探讨了JavaScript中的几种继承方式,包括原型链继承、构造函数继承、组合式继承(含寄生组合),以及ES6引入的类(class)继承。通过实例展示了它们的实现原理和特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

 原型链继承

 构造函数继承

 组合式继承

 寄生组合式继承

 class


 原型:每个函数都会创建一个prototype属性,在原型对象上定义的属性和方法可被对象实例共享

链表 :

 

 原型链继承   Child.prototype = new Father() 

 /**************原型链继承 引用值共享*********************/
        //子类原型指向父类实例
        function Father() {
            this.name = 'lily';
            this.arr = [1, 2, 3, 4]
        }//构造函数
        Father.prototype.getName = function () {
            return this.name
        }
        function Child() { }
        Child.prototype = new Father()      
        const f1 = new Child()//实例
        const c2 = new Child()
        f1.name = 'John'
        f1.arr.push(5)
        console.log('f1', f1, f1.getName())
        console.log('c2', c2, c2.getName())

缺点: 引用值共享

 构造函数继承   Father.call(this)

  /*************构造函数继承 拿不到原型上的方法**********************/
        //子类构造函数中执行父类构造函数,并绑定子类this
        function Father() {
            this.name = 'lily';
            this.arr = [1, 2, 3, 4]
        }
        Father.prototype.getName = function () {
            return this.name
        }
        function Child() {
            Father.call(this)
        }      
        const f1 = new Child()
        const c2 = new Child()
        f1.name = 'John'
        f1.arr.push(5)
        console.log('f1', f1)
        console.log('c2', c2)

缺点:拿不到原型上的方法

 组合式继承   (原型链+构造函数)


        /*************组合式继承**********************/
        function Father() {
            this.name = 'lily';
            this.arr = [1, 2, 3, 4]
        }
        Father.prototype.getName = function () {
            return this.name
        }
        function Child() {
            Father.call(this)//第二次调用 Father()
        }
        Child.prototype = new Father()//第一次调用 Father()
        Child.prototype.constructor = Child
        const f1 = new Child()
        const c2 = new Child()
        f1.name = 'John'
        f1.arr.push(5)
        console.log('f1', f1, f1.getName())
        console.log('c2', c2, c2.getName())

 

缺点:父类构造函数被调用两次 

寄生组合式继承  

Child.prototype = Object.create(Father.prototype)

  /*************寄生组合式继承**********************/
        function Father() {
            this.name = 'lily';
            this.arr = [1, 2, 3, 4]
        }
        Father.prototype.getName = function () {
            return this.name
        }
        function Child() {
            Father.call(this)//绑定Father
        }
        //Object.create()原型式继承,浅拷贝
        Child.prototype = Object.create(Father.prototype)
        Child.prototype.constructor = Child
        const f1 = new Child()
        const c2 = new Child()
        f1.name = 'John'
        f1.arr.push(5)
        console.log('f1', f1, f1.getName())
        console.log('c2', c2, c2.getName())

 

 class

    /*************ES6 class**********************/
        class Father {
            constructor() {
                this.name = 'lily';
                this.arr = [1, 2, 3, 4]
            }
        }
        Father.prototype.getName = function () {
            return this.name
        }
        class Child extends Father {
            constructor() {
                super()
            }
        }     
        const f1 = new Child()
        const c2 = new Child()
        f1.name = 'John'
        f1.arr.push(5)
        console.log('f1', f1, f1.getName())
        console.log('c2', c2, c2.getName())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值