super()是否在后台映射到__proto__? [英] Does super() map to __proto__ under the hood?

查看:64
本文介绍了super()是否在后台映射到__proto__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道ES6中的类实际上是语法糖。 super()调用真的只是在调用 proto 吗? (它是否已映射到[[prototype]]对象?)

I understand that classes in ES6 are really syntactic sugar. Is the super() call really just calling proto? (Is it mapped to the [[prototype]] object?)

推荐答案

不仅仅如此。

const example = {
    method() {
        return super.method();
    }
}

const example = {
    method() {
        return Object.getPrototypeOf(example).method.call(this);
    }
}

class Example {
    method() {
        return super.method();
    }
}

class Example {
    method() {
        return Object.getPrototypeOf(Example.prototype).method.call(this);
    }
}

对于 super() 在构造函数中调用,它类似地在构造函数上使用 Object.getPrototypeOf ,但那里还有更多

As for super() calls in constructors, it similarly uses Object.getPrototypeOf on the constructor, but does a bit more there.


它是否已映射到[[prototype]]对象?

Is it mapped to the [[prototype]] object?

是。不是调用函数的对象的[[prototype]]( this ),而是定义函数的对象的[[prototype]]内。

Yes. Not to the [[prototype]] of the object that the function was called on (this), but to the [[prototype]] of the object that the function was defined in.

这篇关于super()是否在后台映射到__proto__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆