So I want to have an immutable Vector class. For this I need to have a public getter for the x and y coordinates and a private setter so that I can actually initialize those values in the constructor.
I have a few options at my disposal so I'm wondering which one is per convention.
I could do it like this:
class Vector {
constructor(private _x: number, private _y: number) { }
public get x() {
return this._x;
}
public get y() {
return this._y;
}
}
But I don't know if using an underscore is a common thing to do. This might be an issue since that name will be visible in the intellisense.
The second option could be
class Vector {
constructor(private x: number, private y: number) { }
public get X() {
return this.x;
}
public get Y() {
return this.y;
}
}
As far as I know, only classes start with capitals in JS so this might also be a bad idea.
What is the preferred way to handle this?