gpt4 book ai didi

javascript - 在 JavaScript 中获取父类(super class)

转载 作者:行者123 更新时间:2023-12-03 13:32:27 25 4
gpt4 key购买 nike

我正在上课。

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)
}
}
如何访问父类(super class)(而不是属性或方法)?

编辑:
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 }
}
}
是否可以通过数据获得父类(super class)?

最佳答案

这样做很少对您有用,但使用 class 时有几种方法句法:
您可以获得B的原型(prototype):

Object.getPrototypeOf(B) === A // true
之所以有效,是因为 class语法分配 A构造函数作为 B的原型(prototype)构造函数(这很方便,这意味着 BA 继承静态方法)。
或者您可以通过获取 constructor 来完成。 B.prototype 原型(prototype)的属性:
Object.getPrototypeOf(B.prototype).constructor === A // true
或者如果你想使用 B 的实例作为您的起点(在此示例中为 this):
Object.getPrototypeOf(Object.getPrototypeOf(this)).constructor === A // true
现场示例:

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

console.log(Object.getPrototypeOf(B) === A); // true

console.log(Object.getPrototypeOf(B.prototype).constructor === A); // true

// Or to get it from `this`:
console.log(Object.getPrototypeOf(Object.getPrototypeOf(this)).constructor === A); // true
}
}

new B();


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

重新编辑:

Is it possible to get the super class with the data?


我认为您的意思是“是否可以使用数据获取父类(super class)的实例?”答案是肯定的或否定的,这取决于你想如何看待它。 B 的一个实例 A 的一个实例,所以从这个意义上说,"is",因为你已经拥有它,因为你有一个 B 的实例.但是没有单独的 A 实例。那是 仅限 一个 A而不是 B (见上文),所以从这个意义上说,“不”。
AB在您的编辑中,在执行 new B 之前,你在内存中有这样的东西,基本上是两个构造函数 AB及其相关的 A.prototypeB.prototype对象:
               +−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+               |                                                       |               v                                                       |       +−−−−−−−−−−−−−−−+                                               |A−−−−−>| Function A    |  +−>Function.prototype                        |       +−−−−−−−−−−−−−−−+  |                                            |       | [[Prototype]] |−−+                                            |       | name: "A"     |     +−−−−−−−−−−−−−−−+                         |       | prototype     |−−−−>| A.prototype   |                         |       +−−−−−−−−−−−−−−−+     +−−−−−−−−−−−−−−−+                         |               ^             | [[Prototype]] |−−−−−>Object.prototype   |               |             | constructor   |−−−−−−−−−−−−−−−−−−−−−−−−−+               |             +−−−−−−−−−−−−−−−+               +−−−−−−−−−−+          ^                          |          |       +−−−−−−−−−−−−−−−+  |          +−−−−−−−−−−+B−−−−−>| Function B    |  |                     |       +−−−−−−−−−−−−−−−+  |                     |       | [[Prototype]] |−−+                     |       | name: "B"     |     +−−−−−−−−−−−−−−−+  |       | prototype     |−−−−>| B.prototype   |  |       +−−−−−−−−−−−−−−−+     +−−−−−−−−−−−−−−−+  |               ^             | [[Prototype]] |−−+               |             | constructor   |−−+               |             +−−−−−−−−−−−−−−−+  |               |                                |               +−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+

(Some details omitted.)

Now, if you do:

const b = new B();
创建一个 目的。 A构造函数添加 a新对象的属性,以及 B构造函数添加 b该新对象的属性:
+−−−−−−−−−−−−−−−−−+
b−−−−−>| B 的实例 |
+−−−−−−−−−−−−−−−−−+
| [[原型(prototype)]] |−−−−>B.prototype
|答:1 |
| b: 2 |
+−−−−−−−−−−−−−−−−−+

它是单个对象,而不是包含 a 的单独对象和 b . thisA的代码用于 this.a = 1this 是同一个对象那 B的代码用于 this.b = 2 .

关于javascript - 在 JavaScript 中获取父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66290599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com