I'm looking to streamline my JavaScript code, specifically when it comes to constructors.
Currently, I have a vector defined as:
function Vector2(X, Y) {
this.x = 0.0;
this.y = 0.0;
if (X)
this.y = Y;
if (Y)
this.y = Y;
}
At the moment, if I want to add two vectors together, I have to do the following:
var vector1 = new Vector2(1.0, 0.5);
var vector2 = new Vector2(4.5, 1.0);
vector1.x += vector2.x;
vector1.y += vector2.y;
I'm aiming to make the code cleaner, more readable, and reduce file size when using multiple constructors. My goal is to write something like this:
vector1 += vector2;
Any assistance would be greatly appreciated. Thank you.