js 继承的方式

本文深入探讨了JavaScript中的三种继承实现方式:1) Class继承,利用ES6的class和extends关键字实现;2) 原型链继承,通过将子类的prototype指向父类实例来实现;3) call继承,使用Parent.call(this)将父类的上下文绑定到子类实例上。这些方法都是JavaScript中实现继承的重要手段。
摘要由CSDN通过智能技术生成

1:Class 继承

  <script>
    class Parent{
      constructor() {
        this.age = 18
      }
    }
    class Child extends Parent {
      constructor() {
        super()
        this.name = '张三'
      }
    }
    let ol = new Child()
    console.log(ol,ol.name,ol.age);
  </script>

2: 原型链继承

    <script>
      function Parent() {
        this.age = 20;
      }
      function Child() {
        this.name = "张三";
      }

      Child.prototype = new Parent()
      let o = new Child();
      console.log(o, o.name, o.age);
    </script>

3:call 继承

    <script>
      function Parent() {
        this.age = 10;
      }
      function Child() {
        Parent.call(this)
        this.name = "张三";
      }
      let o = new Child();
      console.log(o, o.name, o.age);
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值