Created
November 30, 2017 00:21
-
-
Save domenic/6f0e2fcb2edb7deecb8364a604d75443 to your computer and use it in GitHub Desktop.
Why private static methods are important
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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