What happens when 2 classes both have the same name for a protected member?
(side note, i actually agree / like that you need to use a this.#someMember as opposed to just this.someMember :-) )
What happens when a subclass declares a protected member that has the same name as a parent classes protected member (see example below).
class Point2D {
protected #x = 0;
protected #y = 0;
constructor(x, y) {
var arglen = arguments.length;
if (arglen) {
x = parseFloat(x);
x = (isNaN(x)) ? 0 : x;
if (arglen > 1) {
y = parseFloat(y);
y = (isNaN(y)) ? 0 : y;
}
}
this.#x = x;
this.#y = y;
}
get x() {
return this.#x;
}
get y() {
return this.#y;
}
toString() {
return this.#x + ', ' + this.#y;
}
}
class Point3D extends Point2D {
protected #x = 10; //is this allowed? Or is it checked against the parent class and thereby not allowed?
protected #y = 20;
protected #z = 30;
constructor(x, y, z) {
super(x, y);
//is this.#x = 10 or equal to x passed from the constuctor?
//lets assume it gets set above by the call to super to the value of x passed in.
#x = Math.round(Math.random()*1000);
//which #x? the one defined in this class or the one in the super?
this.#z = z;
}
get z() {
return this.#z;
}
}
var oPoint2D = new Point2D(100, 200),
oPoint3D = new Point3D(500, 600, 700),
nPoint3DX;
nPoint3DX = oPoint3D.x; //Is this 500 (passed via constructor)?
//Or is it 10
The doc seems to imply that a redeclared protected slot would effectively override the slot from the parent
A subclass adds to its lexical slot bindings the binding pairs from its superclass' [[protectedSlotMap]].
But it excludes any binding pairs whose IdentifierName is redeclared by a private or protected declaration within the subclass body.
But if that's true, that means the super class is using a protected value from the sub basically doing an override. That seems like it could be bad. . . if the parent class defined protected members and it's constructor then did the work to set and validate them. . . it's own implementation would be reliant that said validation has occurred. In this case it means it wouldn't have. . .b/c the sub class overwrote. . .
Of course the parent class could just make them private i suppose. . .
Also can protected/private members be non-primitives? What happens with functions/objects? What happens when a protected member is a function and a subclass declares it's own of the same name?