JavaScript Finally Has Support for Native Private Fields and Methods

1. The above proposal allows for private and public fields to exist with the same name at the same time.

2. You cannot access private fields via this['field']

3. This statement is supported.

class Point {
#x;
#y;

constructor(x, y) {
this.#x = x;
this.#y = y;
}
equals(otherPoint) {
return this.#x === otherPoint.#x && this.#y === otherPoint.#y;
}
}

const point1 = new Point(1,2);
const point2 = new Point(2,3);
const point3 = new Point(1,2);
point1.equals(point2);
//false
point1.equals(point3);
//true
class Point {
private int x = 0;
private int y = 0;
public boolean equals(Point p) {
return this.x == p.x && this.y == p.y;
}
}

Author: admin

Leave a Reply

Your email address will not be published.