super.prop
和super[expr]
表达式在类和对象字面量任何方法定义中都是有效的。
语法
super([arguments]); // 调用 父对象/父类 的构造函数 super.functionOnParent([arguments]); // 调用 父对象/父类 上的方法
描述
在构造函数中使用时,super
关键字将单独出现,并且必须在使用this
关键字之前使用。super
关键字也可以用来调用父对象上的函数。
示例
在类中使用super
class polygon { constructor(height, width) { this.name = 'polygon'; this.height = height; this.width = width; } sayName() { console.log('Hi, I am a ', this.name + '.'); } } class Square extends polygon { constructor(length) { this.height; // ReferenceError,super 需要先被调用! /* 这里,它调用父类的构造函数的 length, 作为polygon 的 width和 height. */ super(length, length); /* 注意: 在派生的类中, 在你可以使用'this'之前, 必须先调用super()。 忽略这, 这将导致引用错误。 */ this.name = 'Square'; } get area() { return this.height * this.width; } set area(value) { this.area = value; } }
调用父类上的静态方法
class Human { constructor() {} static ping() { return 'ping'; } } class Computer extends Human { constructor() {} static pingpong() { return super.ping() + ' pong'; } } Computer.pingpong(); // 'ping pong'
原文地址:https://www.cnblogs.com/laneyfu/p/12082641.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。