当前位置: 首页 > news >正文

ES6 class类关键字super

super关键字

        在 JavaSCript 中,能通过 extends 关键字去继承父类

        super 关键字在子类中有以下用法:

  •  当成函数调用 super()
  •  作为 "属性查询" super.prop 和 super[expr]

super()

        super 作为函数调用时,代表父类的构造函数。

        ES6 要求,子类的构造函数必须执行一次 super() 函数。

        注意:作为函数时,super() 只能用在子类的构造函数之中,用在其他地方就会报错。

class A {}
class B extends A {constructor() {super();}
}

        super 作为函数调用时,内部的 this 指的是子类实例。

class A {constructor() {this.print();}print(){console.log('这是A');}
}
class B extends A {constructor() {super();}print(){console.log('这是B');}
}
new B() // 这是B
// 继承了A的print
class A {constructor() {this.print();}print(){console.log('这是A');}
}
class B extends A {constructor() {super();}
}
new B() // 这是A

    

super.prop

  • 在普通方法中,指向父类的原型对象;
  • 在静态方法中,指向父类。

       

        普通方法

        这里的super指向父类原型对象,即 A.prototype

class A {x = 2p() {return this.x;}
}
class B extends A {print(){console.log(super.p())}
}
const a = new B()
a.print() // 2

        由于在普通方法中的 super 指向父类的原型对象,而A的CLASS写法其实是:

function A() {this.x = 2;}A.prototype.print = function () {return this.x
};

        所以我们能够在父类A的原型对象上找到print方法。那么如果是这样:

class A {x = 2
}
class B extends A {print(){console.log(super.x)}
}
const a = new B()
a.print() // undefined

       自2022年之后,实例属性现在除了可以定义在constructor()方法里面的this上面,也可以定义在类内部的最顶层。所以这里的x=2其实等同于constructor(){this.x = 2};

         如果父类上的方法或属性是定义在实例上的,就无法通过 super 调用的

       

         在子类普通方法中通过 super 调用父类的方法时,方法内部的 this 指向的是当前的子类实例。

class A {constructor() {this.x = 1;}print() {console.log(this.x);}
}
class B extends A {constructor() {super();this.x = 2;super.y = 123;  //如果通过super对某个属性赋值,这时super就是this,赋值的属性会变成子类实例的属性。}m() {super.print();}
}
let b = new B();
b.m() // 2
console.log(b.y);  //123

        静态方法

        super作为对象,用在静态方法之中,这时 super 将直接指向父类,而不是父类的原型对象。

class Parent {static myMethod(msg) {console.log('static', msg);}myMethod(msg) {console.log('instance', msg);}
}
class Child extends Parent {static myMethod(msg) {super.myMethod(msg);}myMethod(msg) {super.myMethod(msg);}
}
Child.myMethod(1); // static 1
var child = new Child();
child.myMethod(2); // instance 2

        在子类的静态方法中通过 super 调用父类的方法时,方法内部的 this 指向当前的子类,而不是子类的实例。

class A {constructor() {this.x = 1;}static print() {console.log(this.x);}
}
class B extends A {constructor() {super();this.x = 2;}static m() {super.print();}
}
B.x = 3;
B.m() // 3

http://www.lryc.cn/news/185877.html

相关文章:

  • C++并发与多线程(4) | 传递临时对象作为线程参数的一些问题Ⅰ
  • CentOS Integration SIG 正式成立
  • 智能AI系统源码ChatGPT系统源码+详细搭建部署教程+AI绘画系统+已支持OpenAI GPT全模型+国内AI全模型
  • 软考程序员考试大纲(2023)
  • 【重拾C语言】七、指针(一)指针与变量、指针操作、指向指针的指针
  • Kafka源码简要分析
  • react 按住ctrl键,点击时会出现菜单的问题修复
  • 【虚拟机栈】
  • Linux系列讲解 —— 【fsck】检查并修复Linux文件系统
  • gitlab突然提示我要输入密码了。
  • 业务测试常见问题(一)
  • IntelliJ IDEA失焦自动重启服务的解决方法
  • 终端准入控制系统,保障企业内网安全的关键防线
  • mysql-执行计划
  • 金蝶云星空和旺店通·企业奇门接口打通对接实战
  • 在服务器上使用nginx改变前端项目请求的url
  • 【学习笔记】莫比乌斯反演
  • 一款构建Python命令行应用的开源库
  • 10-Node.js模块化
  • 数字IC前端学习笔记:数字乘法器的优化设计(Dadda Tree乘法器)
  • 计算机专业毕业设计项目推荐14-文档编辑平台(SpringBoot+Vue+Mysql)
  • 【读书后台管理系统】—后端框架搭建(二)
  • 【DLoopDetector(C++)】DBow2词袋模型loop close学习
  • 什么是CAS机制?
  • Java多态详解
  • Android中简单实现Spinner的数据绑定
  • 【版本控制工具二】Git 和 Gitee 建立联系
  • 最新AI智能创作系统ChatGPT商业源码+详细图文搭建部署教程+AI绘画系统
  • 【算法与数据结构】--目录
  • 爱普生LQ1900KIIH复位方法