2
milkmachine = function(argument){ var r=1, k=2; //..... return r; } milkmachine(); //returns r, good milkmachine.set_return_to_another_variable('k'); milkmachine(); //this should return k 
10
  • 1
    Could you try to compose a theoretical example? "return something somewhere else in the code" doesn't make sense to me -- the function returns wherever it was called. You could store the return value and use it later, or you could trigger an event listener elsewhere in the code, maybe. But I can't say for sure what you need because I don't fully understand what you want. Commented Oct 31, 2013 at 19:29
  • 5
    This just looks like a fundamental misunderstanding about how functions work. A function with a return statement will return a value. You access that value later by storing it into a variable so that you can refer to it later. Commented Oct 31, 2013 at 19:38
  • right but can you change what the function will return outside of the function? Commented Oct 31, 2013 at 19:44
  • @KinnardHockenhull So you're asking: is it possible to alter the code of the function, after it's been defined, to make it return whatever is in the internal variable k instead of what's the internal variable r as it was defined to return? Commented Oct 31, 2013 at 19:46
  • 3
    I think the short answer to this question is "No". Commented Oct 31, 2013 at 19:48

1 Answer 1

6

The short answer, as Greg Hewgill commented, is: no (to the best of my knowledge). There are a few shoddy ways you could get that behavior as well as a few recommended ways:

Shoddy Way #1:

One way is to have two versions of the function: one that returns r, the other that returns k. This is not recommended because it is duplicating the same code, just returning something different.

Shoddy Way #2:

Another option would be to add another parameter to the function which would specify the variable you want to be returned:

milkmachine = function(argument, retWhat){ var r; var k; //do something with arguments and variables if (retWhat == "k") return k; else return r; } 

This is also bad since you're using one function to do multiple things and a function should only do one thing; also, it gives external code more info about the inner workings of the function than is good. (There are probably other, better reasons this is bad, but I can't think of any at the moment.)

Recommended Way #1:

Instead of returning just one value, you could return both values wrapped in a container object:

milkmachine = function(argument){ var r; var k; //do something with arguments and variables return new MilkMachineRetrunValue(r, k); } var retval = milkmachine("some arg"); // access retval.r here // access retval.k here // or you could directly access the variable // that you wanted within the return wrapper // after the call like so: // milkmachine("some arg").k 

Recommended Way #2

Use an object:

function MilkMachine() { this.r; this.k; } MilkMachine.prototype.run = function(argument) { //do something with arguments and variables } var mm = new MilkMachine(); mm.run("some arg"); // access mm.r here // access mm.k here 
1
  • 7
    Recommended Way #1 simplified: return {'r': r, 'k': k} Commented Oct 31, 2013 at 20:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.