Use private.x
syntax for referencing private fields
#18
Comments
IMHO, If protected fields might be a future extension, class Foo {
private x;
constructor() {
private.x = 1;
}
} |
Sorry, I understood that class Point {
#x;
#y;
constructor(x = 0, y = 0) {
this.#x = +x;
this.#y = +y;
}
get x() { return this.#x }
set x(value) { this.#x = +value }
get y() { return this.#y }
set y(value) { this.#y = +value }
equals(p) { return this.#x === p.#x && this.#y === p.#y }
toString() { return `Point<${ this.#x },${ this.#y }>` }
} |
However, this function produce the puzzle. class Point2d {
#x;
#y;
constructor(x = 0, y = 0) {
this.#x = +x;
this.#y = +y;
}
}
class Point3d {
#x;
#y;
#z;
constructor(x = 0, y = 0, z = 0) {
this.#x = +x;
this.#y = +y;
this.#z = +z;
}
equals2d(p) { return this.#x === p.#x && this.#y === p.#y }
}
let point2d = new Point2d(1, 2);
let point3d = new Point3d(1, 2, 3);
point3d.equals2d(point2d); // Illigal So, I affirm to use |
private
keywordprivate.x
syntax for referencing private fields
@petamoriken The class Point3d {
#x; #y; #z;
equals2d(p) { return this.#x === p.x & this.#x === p.y }
} Although I'm not sure the example makes much sense to begin with. In any case, you could fix your syntax suggestion by allowing something like: class Point {
private x, y;
equals(other) { return private.x === private(other).x && private.y === private(other).y }
} I considered the
|
@zenparsing Sorry, exactly the example is not good.
Yes but, I propose that private properties should be hidden even on an instance of "the same class".
IMHO, because |
First, private fields aren't really syntax sugar for any WeakMap idioms (although you can you WeakMaps to simulate private fields). They are a user-facing syntax for ES object "internal slots" (which are part of the ES specification). Second, I'm not sure how class-private (as opposed to "instance-private") fields has any bearing on protected state or syntax?
Regardless, my previous two points still stand:
|
It will change the meaning of
That's right, but, using I'm sorry that I should have discussed this issue in wycats/javascript-private-state, I confounded them. |
It changes property lookup rules if the name to the right of the "dot" is a special private field name (i.e. starting with "#" or "@", depending on what we choose). That seems fine to me.
But you're proposing giving "private" a different meaning than is found in similar languages, so you're not actually saving anything conceptually. And it's longer. : ) |
Going to close this for now - cool idea though! |
@zenparsing |
Please see this comment.
The text was updated successfully, but these errors were encountered: