3

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?

6

Doing this is the best (and verbose) option right now.

return {'x': x, 'y': y}

The caller would have to do this to use the values

var c = get_coords();
alert(c.x + ' ' + c.y);    // c.x and c.y holds the coordinates

If you care to use only single axis you could do -

alert(get_coords().x);

With the ECMAScript 6 you would be able to do destructuring (in caller).

var { x, y} = get_coords();

Too bad that we'll have to wait quite long to use ECMAScript 6 safely in production (in a cross-browser manner).

More info on destructuring - http://fitzgeraldnick.com/weblog/50/

3

It depends entirely on what the calling code is expected to do with the returned coordinates. If your current code just needs direct access to the x and y properties, use the literal:

return {x: x, y: y};

If and when you decide you need to "upgrade" the return value, it's pretty effortless to change it to this:

return new Coordinates(x, y);

Or this:

return BuildCoordinate(x, y);

Without preventing legacy code from still working directly with .x and .y, so long as your Coordinates constructor provides them.

And your calling code can effortlessly be "upgraded" to call your special Coordinates methods, like this:

GetCoords().calculateDistanceTo(anotherX, anotherY);

Or perform type-checking, like this:

var coords = GetCoords();
if (coords instanceof Coordinates) {
  coords.plot(canvas);
}

But again, you're probably best off using the bare return value (the object literal) until you have need for the baggage that comes with a "typed" return value (the constructed object).

1

Well, the both approaches are equal, since both are Objects with same properties, just different prototypes.

For the sake of simplicity, you should probably go with the first option. Or even simpler, with the array of the form [x, y].

  • 2
    Why an array? Using an array for this purpose seems bad because you are introducing an implicit assumption that element 0 is x and element 1 is y. – Aaron Kurtzhals Oct 2 '13 at 16:36
  • 1
    Yeah, sure, and in some I am. In mathematics, we have N-dimensonal spaces, in this example 2-D space, first cooridinate is x and second y. In case of 3-D space it might have been [x, y, z]. This way you gain some performace. – Rok Kralj Oct 2 '13 at 16:46
0

Use the one that matches the meaning of your data.

My overwhelming habit is to use simple object literals like dictionaries/hashes and to use custom-prototyped-objects in an OOP manner.

Are you returning a simple collection of convenient values which will be immediately disassembled by the calling function? Then use a quick object literal.

Is your return value a thing that does and represents important conceptual stuff in your data model? Then take the time to write a constructor and make it a prototyped instance.

Sure, javascript lets us be flexible (sloppy) and clever (cryptically) by blending these ideas, but I find my code is much clearer if I stick to this rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.