https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/super

The super keyword is used to access and call functions on an object's parent.

super关键字用于访问和调用一个对象的父对象上的函数。

The super.prop and super[expr] expressions are valid in any method definition in both classes and object literals.

super.propsuper[expr]表达式在对象字面量任何方法定义中都是有效的。

super([arguments]); // calls the parent constructor.调用 父对象/类 的构造函数
super.[functionOnParent]([arguments]); // calls the parent functions 调用 父对象/类 上的方法

Description描述

When used in a constructor, the super keyword appears alone and must be used before the this keyword is used. The super keyword can also be used to call functions on a parent object.

在构造函数中使用时,super关键字将单独出现,并且必须在使用this关键字之前使用。super关键字也可以用来调用父对象上的函数。

Example示例

Using super in classes

Here super() is called to avoid duplicating the constructor parts' that are common between Rectangle and Square.

class Rectangle {
constructor(height, width) {
this.name = 'Rectangle';
this.height = height;
this.width = width;
}
sayName() {
console.log('Hi, I am a ', this.name + '.');
}
get area() {
return this.height * this.width;
}
set area(value) {
this.height = this.width = Math.sqrt(value);
}
} class Square extends Rectangle {
constructor(length) {
this.height; // ReferenceError, super needs to be called first! super需要先被调用 // Here, it calls the parent class' constructor with lengths
// provided for the Rectangle's width and height
super(length, length); // Note: In derived classes, super() must be called before you
// can use 'this'. Leaving this out will cause a reference error.
this.name = 'Square';
}
}

Super-calling static methods

class Rectangle{
constructor() {}
static logNbSides() {
return 'I have 4 sides';
}
} class Square extends Rectangle {
constructor() {}
static logDescription() {
return super.logNbSides() + ' which are all equal';
}
}
Square.logDescription(); // 'I have 4 sides which are all equal'

Deleting super properties will throw an error

You cannot use the 大专栏  JavaScript关键字之super()eb/JavaScript/Reference/Operators/delete">delete operator and super.prop or super[expr] to delete a parent class' property, it will throw a ReferenceError.

你不能使用 delete 操作符super.prop 或者 super[expr] 去删除父类的属性,这样做会抛出 ReferenceError

class Base {
constructor() {}
foo() {}
}
class Derived extends Base {
constructor() {}
delete() {
delete super.foo; // this is bad
}
}
new Derived().delete();
// ReferenceError: invalid delete involving 'super'.

super.prop cannot overwrite non-writable properties

When defining non-writable properties with e.g. Object.defineProperty, super cannot overwrite the value of the property.

当使用 Object.defineProperty 定义一个属性为不可写时,super将不能重写这个属性的值。

class X {
constructor() {
Object.defineProperty(this, 'prop', {
configurable: true,
writable: false,
value: 1
});
}
f() {
super.prop = 2;
}
} var x = new X();
x.f(); // TypeError: "prop" is read-only
console.log(x.prop); // 1

Using super.prop in object literals

Super can also be used in the object initializer / literal notation. In this example, two objects define a method. In the second object, super calls the first object's method. This works with the help of Object.setPrototypeOf() with which we are able to set the prototype of obj2 to obj1, so that super is able to find method1 on obj1.

Super也可以在object initializer / literal 符号中使用。在下面的例子中,两个对象各定义了一个方法。在第二个对象中, 我们使用super调用了第一个对象中的方法。 当然,这需要我们先利用 Object.setPrototypeOf()obj2的原型加到obj1上,然后才能够使用super调用 obj1上的method1

var obj1 = {
method1() {
console.log("method 1");
}
} var obj2 = {
method2() {
super.method1();
}
} Object.setPrototypeOf(obj2, obj1);
obj2.method2();
// logs "method 1"

Specifications规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)super Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)super Living Standard  

JavaScript关键字之super()的更多相关文章

  1. JavaScript 关键字

    JavaScript 关键字 和其他任何编程语言一样,JavaScript 保留了一些关键字为自己所用. JavaScript 同样保留了一些关键字,这些关键字在当前的语言版本中并没有使用,但在以后 ...

  2. JavaScript关键字

    JavaScript关键字 制作人:全心全意 abstract continue finally instanceof private this boolean default float int p ...

  3. IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic、 @synthesize、@property、@dynamic

    IOS 关键字self,super,copy, retain, assign , readonly , readwrite, nonatomic.                     @synth ...

  4. 方法重写和方法重载;this关键字和super关键字

    1:方法重写和方法重载的区别?方法重载能改变返回值类型吗? 方法重写: 在子类中,出现和父类中一模一样的方法声明的现象. 方法重载: 同一个类中,出现的方法名相同,参数列表不同的现象. 方法重载能改变 ...

  5. java 面向对象(十四):面向对象的特征二:继承性 (三) 关键字:super以及子类对象实例化全过程

    关键字:super 1.super 关键字可以理解为:父类的2.可以用来调用的结构:属性.方法.构造器3.super调用属性.方法:3.1 我们可以在子类的方法或构造器中.通过使用"supe ...

  6. javascript关键字和保留字

    1 关键字breakcasecatchcontinuedefaultdeletedoelsefinallyforfunctionifininstanceofnewreturnswitchthisthr ...

  7. 【Java咬文嚼字】关键字(一):super和this

    这段时间一直在学Java,看了辣么多书以及博客,心痒也是着写写自己的学习心得. 这也算是新手篇:咬文嚼字Java中的关键字. 以关键字为第一篇博文也是考虑再三:1.本人基础也是薄弱 2.集跬步至千里 ...

  8. java中this关键字和static关键字和super关键字的用法

    this关键字 1. this 关键字是类内部当中对自己的一个引用,可以方便类中方法访问自己的属性: 2.可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数(这里面上面有个 ...

  9. java面对对象 关键字this super

    this:this是指向对象本身的一个指针,成员函数内部指向当前类的对象 其实this主要要三种用法: 1.表示对当前对象的引用! 2.表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是 ...

随机推荐

  1. UML-设计模式-本地服务容错-代理模式

    在<本地服务容错-适配器+工厂模式>中,总是优先尝试本地服务.但是,有时候需要先尝试外部服务,然后才是本地服务.GoF的代理模式可以解决这个问题. 1.代理模式的一般结构 2.使用代理模式 ...

  2. 基于Docker本地运行Kubernetes

    基于Docker本地运行Kubernetes 概览 下面的指引将高速你如何通过Docker创建一个单机.单节点的Kubernetes集群. 下图是最终的结果: 先决条件 \1. 你必须拥有一台安装有D ...

  3. 【数据库】MyQSL数据完整性

    不知道怎么取标题,简单提一下数据库的完整性实现问题 在MySQL中一种不用写trigger也可以实现级联的方式——直接使用外键实现参照完整性(当然trigger的功能不只是实现级联修改/删除,还可以实 ...

  4. 51nod 1346:递归

    1346 递归 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 函数f(n,m) { 若n=1或m=1返回a[n][m]; 返回f(n-1,m)异或 ...

  5. 2020牛客寒假算法基础集训营5 G街机争霸

    题目描述 哎,又是银首,要是你这个签到题少WA一发就金了 牛牛战队的队员打完比赛以后又到了日常甩锅的时间.他们心情悲伤,吃完晚饭以后,大家相约到一个街机厅去solo.牛牛和牛能进入了一个迷宫,这个迷宫 ...

  6. webapi 传入参数校验

    /// <summary> /// 传入参数校验过滤器 /// </summary> public class ValidateReqModelFilter : ActionF ...

  7. Unity获取游戏对象详解

    我觉得Unity里面的Transform 和 GameObject就像两个双胞胎兄弟一样,这俩哥们很要好,我能直接找到你,你也能直接找到我.我看很多人喜欢在类里面去保存GameObject对象.解决G ...

  8. cpu压测测试--------自己设定cpu需要跑到的压力

    下载压力测试包 https://pan.baidu.com/s/1DJYAzBHHDxMViy5dMel2Lw 提取码:a5j3 使用方法: 方法一:前端启动,按Ctrl+c结束 java -Dbus ...

  9. 不重复,distinct,row_number() over(partition by)

    1.查询不重复的字段 select distinct name from table 2.查询某个字段不重复的,所有内容 sql根据某一个字段重复只取第一条数据 select s.* from ( s ...

  10. 设x,y是概率空间(Ω,F,P)上的拟可积随机变量,证明:X=Y a.e 当且仅当 xdp = ydp 对每个A∈F成立。Q: X=Y almost surely iff ∀A∈G∫AXdP=∫AYdP

    E{XE{Y|C}}=E{YE{X|C}} 现在有没有适合大学生用的搜题软件呢?  https://www.zhihu.com/question/51935291/answer/514312093   ...