【问题标题】:Does super() map to __proto__ under the hood?super() 是否映射到引擎盖下的 __proto__ ?
【发布时间】:2018-04-13 17:08:46
【问题描述】:

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

【问题讨论】:

标签: javascript es6-class


【解决方案1】:

还不止这些。它还会记住方法的定义位置。

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,但does a bit more there

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

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

【讨论】:

    猜你喜欢
    • 2021-10-15
    • 2010-11-03
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多