js中的继承

JS继承实现⽅式也很多,主要分ES5和ES6继承的实现

ES5实现继承主要是基于prototype来实现的,具体有三种⽅法

  • 原型链继承:即 B.prototype=new A()

         function F() {
                this.f = 'zzz'
                this.say = () => console.log(' F i say')
            }
            F.prototype.talk = () => console.log(' F i talk')
    
            var f = new F()
    
            function S() {
                this.s = 'xxx'
                this.speck = () => console.log('S i speck')
            }
            S.prototype = new F()
            S.prototype.tell = () => console.log('S i tell');
            S.prototype.constructor = S
            var s = new S()
            console.log(s)

     

  • 借⽤构造函数继承(call或者apply的⽅式继承) 

  • 组合继承是结合原型链继承和借用构造函数继承

ES6继承

⽤class关键字定义类,⽤extends关键字继承类,⽤super()表示⽗类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值