小编Jen*_*ser的帖子

为什么在JavaScript中(super .__ proto__ === this .__ proto__)是正确的?

似乎在JavaScript(ES6)类中super.__proto__ === this.__proto__。

您能解释为什么会这样吗?在不同的浏览器上,这种行为似乎是一致的,因此我怀疑这是在规范中的某个地方指定的。

考虑以下代码:

class Level1 {
    myFunc() {
        console.log('Level1');
    }
}

class Level2 extends Level1 {
    myFunc() {
        console.log('Level2');
    }
}

class Level3 extends Level2 {
    myFunc() {
        console.log('Level3 BEGIN ' + Math.random()); 
        super.__proto__.myFunc();
        console.log(super.__proto__ === this.__proto__);
        console.log('Level3 END'); 
    }
}

const foo = new Level3();
foo.myFunc();
Run Code Online (Sandbox Code Playgroud)

我本来希望super.__proto__.myFunc();可以调用myFunc()class Level1和that 的功能super.__proto__ !== this.__proto__。相反,super.__proto__.myFunc();实际上是调用myFunc()class Level3(它自己调用),然后在第二次调用时调用myFunc()class Level2。如果super.__proto__ === this.__proto__代码演示的话,这是完全可以理解的。

您能否解释super.__proto__ === …

javascript ecmascript-6

10
推荐指数
1
解决办法
115
查看次数

标签 统计

ecmascript-6 ×1

javascript ×1