Reputation: 132
Preface: I understand that this question probably has an answer somewhere on the internet but I couldn't find a way to formulate it such that google gave me an answer.
Context: I'm working on a small project that involves objects moving around in 2D space. Each of those objects has a position, which is contained in a Vector2D object (has an x coordinate, y coordinate and a few other bits of logic related to positions).
Right now whenever I want to perform any sort of math on these objects (such as, for example, moving one around) , I have to separate out my calculations by X and Y axes which is a massive pain.
On to the question: Is there some way to implement an operator (such as '+' or '*') between classes so don't have to write the separate logic for each separate variable? And if so, what does that look like?
Example: I want to move object A 4 units to the right and 4 down.
Current situation:
this.ObjectA.x += 4;
This.objectA.y += 4;
Ideal situation: this.ObjectA += new Vector2D(4,4);
Upvotes: 3
Views: 1305
Reputation: 12701
Short answer: No, because JavaScript doesn't support operators overloading.
So far, TypeScript didn't implement that feature because that would require type-dependant emits (see TypeScript/issues/5407).
One thing we avoid doing is type-driven emit. The idea is that TypeScript types have no bearing at runtime - the code is as close to the analogous JavaScript that you would have written. Given that, substituting in a method call for an operator would go against that.
Another TS maintainer said they won't implement features which didn't go through ECMAScript proposal process (see TypeScript/issues/2319). The reasoning behind this is to avoid possibble future conflicts with the official ES syntax.
There are a lot of discussions about this feature on esdiscuss.org (some dating 11 years back…). In one of the most recent a following remark has been made:
I recommend to abandon all ideas of operator overloading for arbitrary types - that's extremely unlikely to happen. It's heavily affects optimization possibilities, requires extensive type checks. Moreover, there is no interest from browser vendors to implement it.
Therefore it seems highly unlikely that operators overloading will ever make it to ECMAScript.
Implement a function on the class :) Since operators overloading is just a syntactic sugar you can achieve the same with function calls. So instead of this:
this.ObjectA += new Vector2D(4,4);
do this:
this.ObjectA.addVector(new Vector2D(4,4));
Upvotes: 3
Reputation: 1997
Sounds like you need something similar to the method "add".
Here is an example of this behavior on the Playground site.
Here is the code itself:
class Point {
x: number;
y: number;
constructor(x?: number, y?: number) {
this.x = x ? x : 0;
this.y = y ? y : 0;
}
add(value: Point): void {
this.x += value.x;
this.y += value.y;
}
}
class MovingAroundObject {
position: Point = new Point();
}
const movingObject = new MovingAroundObject();
alert("movingObject.position.x: " + movingObject.position.x + " | movingObject.position.y: " + movingObject.position.y);
// A value to change position of the object
const changePosition = new Point(4, 5);
// Change the position of the object
movingObject.position.add(changePosition);
alert("movingObject.position.x: " + movingObject.position.x + " | movingObject.position.y: " + movingObject.position.y);
// Change the position of the object again
movingObject.position.add(changePosition);
alert("movingObject.position.x: " + movingObject.position.x + " | movingObject.position.y: " + movingObject.position.y);
Upvotes: 1