javascript class

本文探讨了JavaScript中类的构造器、eat方法和super关键字的使用,展示了如何通过原型链实现继承,并指出改变People.prototype.eat()的影响。重点介绍了子类Student如何继承People并扩展功能。
摘要由CSDN通过智能技术生成

class的构造器外部函数eat相当于手动继承写 People.prototype.eat()
改变People.prototype.eat(),会改变内部的class内部的eat
super(name)相当于手动继承 People.prototype.constructor.call(this,name)
People.prototype.constructor == People

 class People {
    constructor(name) {
      this.name = name
    }
    //class的函数eat相当于手动继承写 People.prototype.eat()
    eat() {
      console.log('eat something...');
    }
  }
//改变People.prototype.eat(),会改变内部的class内部的eat
  People.prototype.eat = function () {
    console.log('eat apple');
  }
  class Student extends People {
    constructor(name, number) {
      super(name)//
      //相对于 手动继承方式中的People.prototype.constructor.call(this,name) OR People.call(name)
      this.number = number
    }
    sayHi() {
      console.log(
          this.name,this.number
      );
    }

  }
  const XIALUO = new Student('夏洛', '13')
  XIALUO.sayHi()
  XIALUO.eat()//eat apple
  console.log(XIALUO);
  console.log(People.prototype.constructor == People);//true
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值