You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ES6中super()底层是否映射至__proto__?是否对应[[prototype]]对象?

Understanding super() and Its Relationship to __proto__ and [[Prototype]]

Great questions—let’s unpack these step by step, since ES6 classes are just syntactic sugar over JavaScript’s existing prototype-based inheritance, and super can feel tricky when you look under the hood.

1. Does super() map to __proto__ under the hood?

Short answer: Not directly, but it relies on the prototype chain (which __proto__ exposes).

When you call super() inside a subclass constructor, here’s what actually happens:

  • The JavaScript engine first looks up the [[Prototype]] of the subclass itself (not the subclass instance). For a class Child extends Parent, Child’s [[Prototype]] is Parent (you can verify this with Object.getPrototypeOf(Child) === Parent).
  • It then invokes the constructor of that parent class, binding the current subclass instance (this) to it.

__proto__, on the other hand, is a public accessor property that exposes the [[Prototype]] of an instance (not the class). For example, new Child().__proto__ === Child.prototype, and Child.prototype.__proto__ === Parent.prototype.

So super() doesn’t directly map to an instance’s __proto__—it uses the subclass’s own [[Prototype]] (the parent class) to trigger the parent constructor.

2. Is super() just calling __proto__, and does it correspond to the [[Prototype]] object?

Again, no—super() isn’t "calling __proto__," but it does interact with different [[Prototype]] slots depending on context:

  • In a subclass constructor: super() calls the parent class’s constructor, where the parent class is the subclass’s [[Prototype]]. This is not the same as accessing an instance’s __proto__.
  • In a class method: super refers to the parent class’s prototype object (i.e., Parent.prototype), which is the [[Prototype]] of the subclass’s prototype (Child.prototype.__proto__ === Parent.prototype). When you do super.method(), it’s equivalent to Parent.prototype.method.call(this).

To clarify the difference between the two contexts, here’s a concrete example:

class Parent {
  constructor() {
    this.name = "Parent";
  }
  greet() {
    console.log(`Hello from ${this.name}`);
  }
}

class Child extends Parent {
  constructor() {
    super(); // Calls Parent constructor, binds `this` to Child instance
    this.name = "Child";
  }
  greet() {
    super.greet(); // Uses Parent.prototype.greet, binds `this` to Child instance
  }
}

const kid = new Child();
kid.greet(); // Logs "Hello from Child"

In the constructor, super() triggers the parent’s constructor logic to initialize the instance. In the method, super points to the parent’s prototype, allowing you to call inherited methods while preserving the current instance’s this context.

Remember: [[Prototype]] is the internal slot that defines an object’s position in the prototype chain. __proto__ is just a way to access that internal slot from user code. super leverages the prototype chain but doesn’t directly "call" __proto__—it interacts with the parent class or parent prototype based on where it’s used.

内容的提问来源于stack exchange,提问作者MsGirlPerl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:35:59