在 JavaScript 中获取超类

Und*_*Der 7 javascript

我正在上课。

class A {
    constructor() {
    }
    getThis() {
        return this;
    }
}
class B extends A {
    constructor() {
        super();

        this.superclass = super;  // SyntaxError: 'super' keyword unexpected here
        this.superclass = super.getThis();  // this.superclass is this(B and not A)
    }
}
Run Code Online (Sandbox Code Playgroud)

如何访问超类(不是属性或方法)?


编辑:

class A {
    constructor() {
        this.a = 1;
    }
    getThis() {
        return this;
    }
}
class B extends A {
    constructor() {
        super();
        this.b = 2;

        console.log( [get the super class] );  // A { a: 1 } 
        console.log(this);  // B { a: 1, b: 2 }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有可能用数据获取超类?

T.J*_*der 6

这样做对您没有任何用处的情况很少见,但是在使用class语法时有几种方法:

您可以获得以下原型B

Object.getPrototypeOf(B) === A // true
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为class语法将A构造函数指定为构造函数的原型B(这很方便,这意味着B从 继承静态方法A)。

或者您可以通过获取 constructor 的原型的属性来实现B.prototype

Object.getPrototypeOf(B.prototype).constructor === A // true
Run Code Online (Sandbox Code Playgroud)

或者,如果您想使用 istanceB作为起点(this在本例中):

Object.getPrototypeOf(Object.getPrototypeOf(this)).constructor === A // true
Run Code Online (Sandbox Code Playgroud)

现场示例:

Object.getPrototypeOf(B) === A // true
Run Code Online (Sandbox Code Playgroud)


不过,你getThis暗示了一个误解。当您new B创建 的实例时B不会创建一个单独的对象,它只是 的一个实例Anew B创建的一个对象是继承和初始化的特性AB结果的组合。没有单独的A实例。(有A.prototype,但那不是 的实例A,它只是用作 的实例的原型的对象A。)


重新编辑:

有没有可能用数据获取超类?

我认为您的意思是“是否可以使用数据获取超类的实例?” 答案是或否取决于您想如何看待它。的实例B 的实例A,因此从这个意义上说,“是”,因为您已经拥有它,因为您拥有 的实例B。但是没有单独的实例A只有一个A而不是一个B(见上文),所以从这个意义上说,“不”。

使用AandB在你的编辑中,在做之前new B,你在内存中有这样的东西,基本上是两个构造函数AandB和它们关联的A.prototypeandB.prototype对象:

               +??????????????????????????????????????????????????? ??????+
               | |
               v |
       +???????????????+ |
A?????>| 功能 A | +?>Function.prototype |
       +???????????????+ | |
       | [[原型]] |??+ |
       | 名称: "A" | +???????????????+ |
       | 原型|????>| A.prototype | |
       +???????????????+ +???????????????+ |
               ^ | [[原型]] |?????>Object.prototype |
               | | 构造函数|??????????????????????????????+
               | +???????????????+
               +????????????+ ^
                          | |
       +???????????????+ | +????????????+
B?????>| 功能 B | | |
       +???????????????+ | |
       | [[原型]] |??+ |
       | 名称: "B" | +???????????????+ |
       | 原型|????>| B.原型| |
       +???????????????+ +???????????????+ |
               ^ | [[原型]] |??+
               | | 构造函数|??+
               | +???????????????+ |
               | |
               +?????????????????????????????????????+

(省略了一些细节。)

现在,如果你这样做:

const b = new B();
Run Code Online (Sandbox Code Playgroud)

创建单个对象。该A构造增加了一个a属性,以新的对象,而B构造函数添加一个b属性到新的对象:

       +???????????????+
b?????>| B的实例|
       +???????????????+
       | [[原型]] |????>B.prototype
       | 答:1 |
       | 乙:2 |
       +???????????????+

它是一个单独的对象,而不是包含a和的单独对象b。在thisA“s中的代码的用途this.a = 1是相同的对象thisB”在S码的用途this.b = 2