super()是否在后台映射到__proto__? [英] Does super() map to __proto__ under the hood?
问题描述
我知道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屋!