Often I am writing a function that returns multiple values, such as a pair of coordinates:
function get_coords() {
return ???;
}
If I was using a language like Python, I would use a tuple:
return (x, y)
Or, in an OOP language like Java, I would create a custom coordinate class:
class Coordinates {
public int x;
public int y;
public Coordinates(x, y) {
this.x = x;
this.y = y;
}
}
Then return like this:
return new Coordinates(x, y);
On to JavaScript.
In JavaScript, we have the option of returning a simple object:
return {x: x, y: y}
or a Java-like approach, with a custom object:
return new coordinates(x, y)
Are there any reasons to use one above the other?