class ClassWithPrivateField {
#privateField;
constructor() {
this.#privateField = 42;
}
}
class SubClass extends ClassWithPrivateField {
#subPrivateField;
constructor() {
super();
this.#subPrivateField = 23;
}
}
new SubClass();
//Object { #privateField: 42, #subPrivateField: 23 }
Question 1 - Why Parent Class private variables are able to access in the Child Class in JavaScript.
Issue is replicating in both Chrome and Firefox.
MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields
Is it a bug in chrome browser and Firefox browser?
Question 2 - In Chrome browser, I am able to access the private variables, where as in Firefox Browser, I am not able to access the private variables.
In the above example:
var subClass = new SubClass();
subClass.##privateField; //value is printing in chrome and Firefox is throwing "Uncaught SyntaxError: reference to undeclared private field or method #privateField".
Is it a bug in chrome browser?
Please provide a detail explanation with an example for both Question 1 and 2.