Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Why private static methods are important
// Today, with underscore-prefixed "private"
class Point {
constructor(x, y) {
this._x = x;
this._y = y;
}
length() {
return Math.sqrt(squareLengthHelper(this));
}
}
function squareLengthHelper(point) {
return point._x ** 2 + point._y ** 2;
}
export default Point;
// In the future, with real private
class Point {
#x, #y;
constructor(x, y) {
this.#x = x;
this.#y = y;
}
length() {
return Math.sqrt(Point.#squareLengthHelper(this));
}
static #squareLengthHelper(point) {
return point.#x ** 2 + point.#y ** 2;
}
}
// ok, but after writing this, I realized that #squareLengthHelper is better as an instance method...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.