-
Notifications
You must be signed in to change notification settings - Fork 114
Question about nested classes and shadowing #126
Comments
Yes, it’s impossible for B to refer to A’s foo in your example. However, since they’re both private, you can freely rename one of them without it being observable to any consumers. |
Chrome’s behavior is correct. |
@jridgewell I understand that @ljharb why is the shadowing happening? Can you point to part of the spec? I couldn't figure it out. Thanks for your help. |
@mheiber it's just that const foo = "A's #foo";
const bar = "A's #bar";
class A {
get [foo]() { return 1; }
get [bar]() { return 2; }
get ["B's foo"]() { throw new Error("A does not have B's foo"); } // implicit with private fields
method() {
const foo = "B's foo";
class B {
get [foo]() { return 3; }
get [bar]() { throw new Error('B does not have bar'); } // implicit with private fields
methodNested(a) {
console.log('log [foo] from B', this[foo]);
console.log('log a’s [bar] from B', a[bar]);
console.log('errors on the next line');
console.log('log a[foo] from B', a[foo]);
}
}
new B().methodNested(new A());
}
}
new A().method(); (note that i typed this without trying it in the repl; i'm hoping it will run, but that either way you get the gist) |
I agree with the above analysis. @gsathya I wonder if it's possible to improve the error message here. |
Filed https://bugs.chromium.org/p/v8/issues/detail?id=8144. Can you recommend a better error message? |
I posted a suggestion in in the Chrome issue tracker: https://bugs.chromium.org/p/v8/issues/detail?id=8144#c3. See also this Gist with a rough sketch for how to distinguish the ways a.#foo property accesses expressions can fail: https://gist.github.com/mheiber/03ca6b200fdd2121d24674f19fbc5db7#file-error-cases-for-private-names-md. I'm not sure if the sketch is correct. |
closing the issue, since the original question is answered |
Nested Classes Example
In the following code:
Chrome Canary with experimental JS flags fails on the last
console.log
:Question
Is Chrome Canary's behavior to spec? Is it the case that
B
's#foo
shadowsA
's#foo
?The text was updated successfully, but these errors were encountered: