似乎在 JavaScript (ES6) Classes 中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();我原以為super.__proto__.myFunc();會調用myFunc()class的函數,Level1而super.__proto__ !== this.__proto__. 相反,super.__proto__.myFunc();實際上調用myFunc()class Level3(它調用自己),然后在第二次調用時調用myFunc()class Level2。如果super.__proto__ === this.__proto__代碼演示了這一點,這是完全可以理解的。你能解釋一下super.__proto__ === this.__proto__這個例子中的原因嗎?如果可能,還請提供對規范相關部分的參考。
為什么在 JavaScript 中是真的?
一只甜甜圈
2021-11-18 15:42:37