From 01b0e12ba27b5069248fd09235e9a7143915ee30 Mon Sep 17 00:00:00 2001
From: Irvin 以下代码片段来自于 classes sample。 你也可以用 super 调用父类的静态方法。 你不能使用 delete 操作符 加 语法
-super([arguments]);
+
super([arguments]);
// 调用 父对象/父类 的构造函数
super.functionOnParent([arguments]);
@@ -33,7 +33,7 @@ super.functionOnParent([arguments]);
class Polygon {
+
class Polygon {
constructor(height, width) {
this.name = 'Rectangle';
this.height = height;
@@ -69,7 +69,7 @@ class Square extends Polygon {
class Rectangle {
+
class Rectangle {
constructor() {}
static logNbSides() {
return 'I have 4 sides';
@@ -88,7 +88,7 @@ Square.logDescription(); // 'I have 4 sides which are all equal'
super.prop
或者 super[expr]
去删除父类的属性,这样做会抛出 {{jsxref("ReferenceError")}}。class Base {
+
class Base {
constructor() {}
foo() {}
}
@@ -105,7 +105,7 @@ new Derived().delete(); // ReferenceError: invalid delete involving 'super'.当使用 {{jsxref("Object.defineProperty")}} 定义一个属性为不可写时,
super
将不能重写这个属性的值。
class X { +class X { constructor() { Object.defineProperty(this, 'prop', { configurable: true, @@ -132,7 +132,7 @@ console.log(y.prop); // 1-
Super
也可以在object initializer / literal 符号中使用。在下面的例子中,两个对象各定义了一个方法。在第二个对象中, 我们使用super
调用了第一个对象中的方法。 当然,这需要我们先利用 {{jsxref("Object.setPrototypeOf()")}} 设置obj2
的原型为obj1
,然后才能够使用super
调用obj1
上的method1
。var obj1 = { +var obj1 = { method1() { console.log("method 1"); } -- cgit v1.2.3-54-g00ecf