Skip to content

Instantly share code, notes, and snippets.

@domenic
Created November 30, 2017 00:21
Show Gist options
  • Save domenic/6f0e2fcb2edb7deecb8364a604d75443 to your computer and use it in GitHub Desktop.
Save domenic/6f0e2fcb2edb7deecb8364a604d75443 to your computer and use it in GitHub Desktop.
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