• ES6类和继承


    一、类

    在javascript语言中,生成实例对象使用构造函数;ES6提供了类Class这个概念,作为对象的模板。定义一个类通过class关键字,ES6的类可以看成是构造函数的另一种写法。

    如何定义一个类?

    1、我们通过class关键字来定义一个类:

    1. class Person{
    2. // 类的构造器(必须提供,如果不写,默认提供空的构造器)
    3. constructor(name,age,weight){
    4. // 实例私有属性
    5. this.name = name;
    6. this.age = age;
    7. this.weight = weight;
    8. }
    9. // 公共方法 ——类似于存在原型对象中
    10. // Person.prototype
    11. sayName(){
    12. console.log(this.name);
    13. }
    14. // 引用数据类型->实例私有属性
    15. test = [];
    16. // 基本数据类型->实例公共属性
    17. test1 = 'hello';
    18. // 静态属性 由static声明的属性就是静态属性
    19. static personAttr = '静态属性';
    20. // 静态方法 由static声明的属性就是静态方法
    21. static personMethod(){
    22. console.log('这是我们的静态方法');
    23. }
    24. }
    25. let p1 = new Person('张三',15,'50kg')
    26. console.log(p1); //Person { name: '张三', age: 15, weight: '50kg' }
    27. p1.sayName()//张三
    28. let p2 = new Person()
    29. console.log(p1.sayName === p2.sayName);//true
    30. // 静态属性和方法:只能由类本身去调用
    31. console.log(Person.personAttr); //静态属性
    32. Person.personMethod() //这是我们的静态方法
    33. console.log(p1.test === p2.test);//false
    34. console.log(p1.test1 === p2.test1);//true

    2、constructor方法是类的默认方法,通过new命令生成对象实例时,自动调用该方法。一个类必须有constructor方法,如果没有显式定义,一个空的constructor方法会被默认添加。constructor里面定义的属性属于实例的私有属性。

    3、定义在类体的方法称为实例方法,其实是存在于Person.prototype中,可供所有的实例调用。比如上述的sayName方法就属于公共方法,可供所有实例使用。

    定义在类体的属性:如果是引用数据类型,就属于实例的私有属性,比如上述的test;如果是基本数据类型,就属于实例的公共属性,比如上述的test1。

    4、通过static关键字来定义静态属性和静态方法。静态属性和静态方法是定义在类上的,所以只能由类本身去调用。在静态方法中,this指向当前类。

    二、类的继承

    ES5实现继承的三种方式https://blog.csdn.net/LQ313131/article/details/126432548

    class可以通过extends关键字实现继承,子类可以没有构造函数,系统会默认分配。子类提供了构造函数则必须要显式调用super。super函数类似于借用构造函数。

    实现继承之后:

    1、子类对象指向父类对象

    console.log(Dog.__proto__ === Animal); //true

    2、子类原型对象继承父类原型对象

    console.log(Dog.prototype.__proto__ === Animal.prototype); //true

    1. class Animal{
    2. constructor(name,age,weight){
    3. // console.log('父类构造器');
    4. this.name = name;
    5. this.age = age;
    6. this.weight = weight;
    7. }
    8. sayName(){
    9. console.log(this.name);
    10. }
    11. static animalAttr = '父类静态方法'
    12. static animalMethods(d){
    13. return d instanceof Animal
    14. }
    15. }
    16. // 继承 extends关键字继承 必须显式调用super()
    17. class Dog extends Animal{
    18. constructor(name,age,weight,type){
    19. //使用super调用父类构造器
    20. super(name,age,weight)
    21. // console.log('子类构造器');
    22. this.type = type
    23. }
    24. sayName(){
    25. console.log('这是子类公共方法');
    26. }
    27. }
    28. let d1 = new Dog('可乐',12,'10kg','金毛')
    29. console.log(d1);//Dog { name: '可乐', age: 12, weight: '10kg', type: '金毛' }
    30. //子类的原型对象继承父类的原型对象
    31. d1.sayName()//这是子类公共方法
    32. //子类的对象通过__proto__指针指向父类的对象
    33. console.log(Dog.animalAttr);//父类静态方法
    34. console.log(Dog.animalMethods(d1));//true

  • 相关阅读:
    js 深度学习(六)
    2022-09-16 Android app 让图片在ScrollView里面等比例完整显示不变形,继承ImageView ,对ImageView 进行修改。
    Python 中最常用的 4种股票价格移动平均方法(二)
    三大数据库 sequence 之华山论剑 (下篇)
    vs2019 c++20 规范 STL库中关于时间的模板 ratio<T,U> , duration<T,U> , time_point<T,U>等
    Kubernetes部署kubernates Nginx Ingress Controller
    编程之美1 让CPU占用率曲线听你指挥
    java高级:动态代理
    好物周刊#7:炫酷的浏览器标签页
    openjudge 1.8.24 蛇形填充数组
  • 原文地址:https://blog.csdn.net/lq313131/article/details/126872873