Skip to main content
  1. About
  2. For Teams
Asked
Viewed 15k times
9

Is there a way to use a sort of the C#-like out or ref parameter modifiers with Javascript to do something like this:

function myManyReturnFunction(number1, number2, out x, out y) {
    x = number1 * number2;
    y = number1 / number2;

    return true;
}

var height1, height2 = 0;
var check = myManyReturnFunction(1,1, out height1, out hight2);

I would like to change the variable's reference as well. So yes, passing an argument by reference.

5
  • 1
    so something like pass by reference?
    KJYe.Name
    –  KJYe.Name
    2011-02-08 19:58:49 +00:00
    Commented Feb 8, 2011 at 19:58
  • 1
    I believe something along the lines of returning an associative array would be the javascript way to go, but if you're trying to replicate pass by reference this might be interesting: sirdarckcat.blogspot.com/2007/07/…
    kloffy
    –  kloffy
    2011-02-08 20:02:19 +00:00
    Commented Feb 8, 2011 at 20:02
  • @kloffy that blog post is full of errors and misunderstandings - I wouldn't rely on it for much of anything
    Pointy
    –  Pointy
    2011-02-08 20:22:24 +00:00
    Commented Feb 8, 2011 at 20:22
  • @Pointy I must admit, I haven't actually tested the code. The explanation seemed reasonable, though. Care to point out the errors?
    kloffy
    –  kloffy
    2011-02-08 20:29:30 +00:00
    Commented Feb 8, 2011 at 20:29
  • @kloffy well it'd take a pretty long post :-) Basically, the author seems not to have known very much about Javascript. The article is almost 4 years old though.
    Pointy
    –  Pointy
    2011-02-08 20:40:09 +00:00
    Commented Feb 8, 2011 at 20:40

5 Answers 5

19
function myManyReturnFunction(number1, number2) {
    return {
        x: number1 * number2,
        y: number1 / number2
    }
}

And you don't need x and y parameters, simply call:

var result = myManyReturnFunction(6, 9);
var x = result.x;
var y = result.y;
Sign up to request clarification or add additional context in comments.

Comments

3

You can create an "object"...

function getObj(){
var objOut = new Object();
objOut.name = "Smith";
objOut.State = "Arkansas";

return objOut;
}

Comments

3

If you define the 'out' variables in the global scope outside if your function they can be reassigned inside the function with something like this:

var height1 = 0, height2 = 0;

function myManyReturnFunction(number1, number2) {
    height1 = number1 * number2;
    height2 = number1 / number2;

    return true;
}


var check = myManyReturnFunction(1,1);

You can also create an object, which you can then pass to your function, which can be modified within the function like so:

var myValues = {}

function setValues(num1, num2, vals) {
  vals.x = num1 * num2;
  vals.y = num1 / num2;

  return True;
}

setValues(1, 1, myValues);

Comments

2

There are several ways to return multiple values in JavaScript. You've always been able to return multiple values in an array:

function f() {
    return [1, 2];
}

And access them like this:

var ret = f();
document.write(ret[0]); // first return value

But the syntax is much nicer in JavaScript 1.7 with the addition of destructuring assignment (if you're lucky enough to be targeting an environment guaranteed to support it (e.g. a Firefox extension)):

var a, b;
[a, b] = f();
document.write("a is " + a + " b is " + b + "<br>\n");

Another option is to return an object literal containing your values:

function f() {
    return { one: 1, two: 2 };
}

Which can then be accessed by name:

var ret = f();
document.write(ret.one + ", " + ret.two);

And of course you could do something really horrible like modify global scope or even set properties on the function itself:

function f() {
    f.one = 1;
    f.two = 2;
}

f();

document.write(f.one + ", " + f.two);

More reading (and the source of some of these examples):

https://developer.mozilla.org/en/New_in_JavaScript_1.7#Destructuring_assignment_(Merge_into_own_page.2fsection)

Comments

0

You can return non-primitive types:

function blah() {
  return { name: 'john', age: 23 };
};

var x = blah();

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.